题解 | 密码验证合格程序

密码验证合格程序

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

#include <iostream>
using namespace std;

bool isStyle(string s){
    bool hasCapitalLetter = false;
    bool hasSmallLetter = false;
    bool hasDigital = false;
    bool hasSpecialCharacter = false;
    int count = 0;
    for(const auto &c : s){
        if(c >= 'A' && c <= 'Z') {
            if(!hasCapitalLetter){
                count ++ ;
            }
            hasCapitalLetter = true;
        }
        if(c >= 'a' && c <= 'z'){
            if(!hasSmallLetter){
                count ++ ;
            }
            hasSmallLetter = true;
        }
        if(c >= '0' && c <= '9'){
            if(!hasDigital){
                count ++ ;
            }
            hasDigital = true;
        }
        if(c < '0' || c > '9' && c < 'A' || c > 'Z' && c < 'a' || c > 'z'){
            if(!hasSpecialCharacter){
                count ++ ;
            }
            hasSpecialCharacter = true;
        }
    }
    return count >= 3;
}

/**
 *  暴力
 *  如果存在两个长度大于2的独立子串返回true
 */
bool isSubStr(string s){
    for(int i = 0; i < s.size() - 2; i += 1){
        for(int j = i + 3; j < s.size() - 2; j += 1){
            // cout << s[i] << s[i + 1] << " " << s[j] << s[j + 1] << "\n";
            if(s[i] == s[j] && s[i + 1] == s[j + 1] && s[i + 2] == s[j + 2]){
                return true;
            }
        }
    }
    return false;
}

int main() {
    string s;
    while(cin >> s){
        if(s.size() <= 8){
            cout << "NG\n";
            continue;
        }
        if(!isStyle(s)){
            cout << "NG\n";
            continue;
        }
        if(isSubStr(s)){
            cout << "NG\n";
            continue;
        }
        cout << "OK\n";
    }
    return 0;
}
// 64 位输出请用 printf("%lld")
全部评论
点赞 回复 分享
发布于 02-20 21:54 北京

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务