题解 | 密码验证合格程序

密码验证合格程序

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

#include<bits/stdc++.h>
using namespace std;
bool isP(const string& password) {
    int n = password.length();
    // 检查所有可能的子串对
    for (int len = 3; len <= n/2; ++len) {       // 子串长度从3开始
        for (int i = 0; i <= n - len; ++i) {      // 第一个子串起始位置
            string s1 = password.substr(i, len);
            // 检查后续是否出现相同子串(不重叠)
            for (int j = i + len; j <= n - len; ++j) {
                string s2 = password.substr(j, len);
                if (s1 == s2) {
                    return false;  // 发现非法子串
                }
            }
        }
    }
    return true;
}
int main (){
    string a;
    while(cin>>a){
        int c = 0;
        int b[] = {0,0,0,0};//b[0]大写字母 b[1]小写字母 b[2]数字 b[3]特殊字符
        for(int i = 0;i<a.length();i++){
            if(a[i]>=48&&a[i]<=57){
                b[2] = 1;
            }
            if(a[i]>=65&&a[i]<=90){
                b[0] = 1;
            }
            if(a[i]>=97&&a[i]<=122){
                b[1] = 1;
            }
            if(a[i]>=33&&a[i]<=47||a[i]>=58&&a[i]<=64||a[i]>=91&&a[i]<=96||a[i]>=123&&a[i]<=126){
                b[3] = 1;
            }
        }
        for(int i = 0; i<4; i++){
            if(b[i]==1){
                c = c + 1;
            }
        }
        bool d = isP(a);
        if(a.length()<=8){
            cout<<"NG"<<"\n";
            continue;
        }else if(c<3){
            cout<<"NG"<<"\n";
            continue;
        }else if(d){
            cout<<"OK"<<"\n";
        }else{
            cout<<"NG"<<"\n";
        }
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 12:22
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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