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

密码验证合格程序

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

#include <bits/stdc++.h>
#include <vector>
using namespace std;
//判断字串是否重复
bool checkSub(string &s){
    unordered_set<string> set;
    for(int i = 2; i < s.size(); ++i){
        auto str = s.substr(i, 3);
        auto it = set.find(str);
        if(it == set.end()){
            set.insert(str);
        }else{
            return false;
        }
    }

    return true;
}
bool check(string & s){
    vector<int> flag(4, 0);
    int count = 0;
    if(s.size() <= 8){
        return false;
    }
    //判断是否包含四种类型中的三个
    for(int i = 0; i < s.size(); ++i){
        if(s[i] >= 'a' && s[i] <= 'z'){
            flag[0] = 1;
        }else if(s[i] >= 'A' && s[i] <= 'Z'){
            flag[1] = 1;
        }else if(s[i] >= '0' && s[i] <= '9'){
            flag[2] = 1;
        }else{
            flag[3] = 1;
        }
        if(flag[0] + flag[1] + flag[2] + flag[3] >=3) break;
    }
    //检查字串
    if(!checkSub(s)){
        return false;
    }
    //统计密码类型
    for(int i = 0; i < 4; ++i){
        if(flag[i] == true){
            count++;
        }
    }
    return count >= 3;
}
int main() {
    vector<string> str;
    string s;
    while(cin >> s){
        str.push_back(s);
    }
    for(auto i : str){
        if(check(i)){
            cout << "OK" << endl;
        }else{
            cout << "NG" << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务