牛客-华为机考-HJ20 密码验证合格程序

// https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
// 牛客-华为机考-HJ20 密码验证合格程序

#include <iostream>
#include <string>
#include <regex>

using namespace std;


int GetPWDTypeNum(const string pwd)
{
    int pwdTypeNum = 0;
    if (regex_search(pwd, regex("[a-z]"))) {
        pwdTypeNum++;
    }
    if (regex_search(pwd, regex("[A-Z]"))) {
        pwdTypeNum++;
    }
    if (regex_search(pwd, regex("[0-9]"))) {
        pwdTypeNum++;
    }
    if (regex_search(pwd, regex("[^0-9a-zA-Z\n ]"))) {
        pwdTypeNum++;
    }

    return pwdTypeNum;
}

bool PWDComplexityVerification(const string pwd)
{
    bool ispwdok = true;
    int pwdLen = pwd.length();
    int minVfyLen = 3;
    int maxVfyLen = pwdLen / 2;
    for (int vfyLen = minVfyLen; vfyLen < maxVfyLen; vfyLen++) {
        for (int cnt = 0; cnt < pwdLen - vfyLen * 2; cnt++) {
            string vfyStr = pwd.substr(cnt, vfyLen);
            string vfySubStr = pwd.substr(cnt + vfyLen);
            if (vfySubStr.find(vfyStr) != vfySubStr.npos) {
                ispwdok = false;
                break;
            }
        }

        if (!ispwdok) {
            break;
        }
    }

    return ispwdok;

}

int main()
{
    string pwd {};

    while (cin >> pwd) {
        if (pwd.length() < 8 ||
            GetPWDTypeNum(pwd) < 3 ||
            !PWDComplexityVerification(pwd)) {
            cout <<"NG" <<endl;
            continue;
        }
        cout <<"OK" <<endl;

    }
    return 0;
}

 

posted @ 2022-05-06 12:35  天高云肆  阅读(161)  评论(0)    收藏  举报