题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

#include <iostream>
#include <string>
#include <unordered_set>

const std::string OK = "OK";
const std::string NG = "NG";

bool check1(const std::string& s) {
    return s.length() > 8;
}

bool check2(const std::string& s) {
    int type[4] = {0, 0, 0, 0};
    for (auto& i : s) {
        if (i == ' ' || i == '\n') {
            return false;
        }
        if (i >= 'A' && i <= 'Z') {
            type[0] = 1;
        } else if (i >= 'a' && i <= 'z') {
            type[1] = 1;
        } else if (i >= '0' && i <= '9') {
            type[2] = 1;
        } else {
            type[3] = 1;
        }
    }
    if (type[0] + type[1] + type[2] + type[3] < 3) {
        return false;
    }
    return true;
}

bool check3(const std::string& s) {
    std::unordered_set<std::string> sets;
    std::string tmp;
    for (int i = 0; i < s.length() - 2; ++i) {
        tmp = s.substr(i, 3);
        if (sets.find(tmp) == sets.end()) {
            sets.insert(tmp);
        } else {
            return false;
        }
    }
    return true;
}

int main() {
    std::string in;
    while (getline(std::cin, in)) {
        if (check1(in) && check2(in) && check3(in)) {
            std::cout << OK << '\n';
        } else {
            std::cout << NG << '\n';
        }
    }
    return 0;
}

有看到一个利用正则表达式匹配的解法

#include <regex>

记录一下

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务