分别验证密码的长度、符号种类、是否有长度大于2的子串重复判断是否合格。符号种类可以使用set的不可包含重复元素的特性,子串重复的判断,可以判断长度从3到密码长度一般的子串是否可用find函数找到。

#include <iostream>
#include <set>
using namespace std;

int symbolNum(const string & s);

int main() {
    string s;
    bool conti = false;
    while (cin >> s) {
        if (s.length() <= 8) {
            cout << "NG" << endl;
            continue;
        }
        if (symbolNum(s) < 3) {
            cout << "NG" << endl;
            continue;
        }
        for (int i = 3; i <= s.length() / 2; i++) {
            for (int j = 0; j <= s.length() - i; j++) {
                if (s.find(s.substr(j, i), j + 1) != string::npos) {
                    cout << "NG"<< endl;
                    conti = true;
                    break;
                }
            }
            if (conti == true) {
                break;
            }
        }
        if (conti == true) {
            conti = false;
            continue;
        }
        cout << "OK" << endl;
    }
}

int symbolNum(const string & s) {
    set<int> set;
    for (char c : s) {
        if (isdigit(c)) {
            set.insert(1);
        } else if (c >= 'a' && c <= 'z') {
            set.insert(2);
        } else if (c >= 'A' && c <= 'Z') {
            set.insert(3);
        } else {
            set.insert(4);
        }
    }
    return set.size();
}
// 64 位输出请用 printf("%lld")