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

密码验证合格程序

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

思路很清晰,唯一的难点就是包含公共元素的子串重复的问题

#include <array>
#include <iostream>
#include <string>
using namespace std;

bool RepeatSubString(string pwd) {
    // TODO:不能有长度大于2的包含公共元素的子串重复 有=true 没有=false
    for (int i = 0; i + 3 < pwd.size(); i++) {
        string d = pwd.substr(i, 3);
        if (pwd.find(d, i + 3) != -1) return true;
    }
    return false;
}
bool VerifyPavsswordValidity(string pwd) {
    int typeNumber = 0;
    bool bType[4] {false};
    if (pwd.length() <= 8) return false;
    if (RepeatSubString(pwd)) return false;
    for (auto c : pwd) {
        if ('a' <= c && c <= 'z') {
            bType[0] = true;
            continue;
        }
        if ('A' <= c && c <= 'Z') {
            bType[1] = true;
            continue;
        }
        if ('0' <= c && c <= '9') {
            bType[2] = true;
            continue;
        }
        bType[3] = true;
    }
    for (auto item : bType) {
        if (item) {
            typeNumber++;
        }
    }
    return typeNumber >= 3;
}

int main() {
    string password;
    while (getline(cin, password)) { // 注意 while 处理多个 case
        if (VerifyPavsswordValidity(password)) {
            cout << "OK" << endl;
        } else {
            cout << "NG" << endl;
        }

    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

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