题解 | 密码验证合格程序

密码验证合格程序

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

#include <iostream>
#include <string>
 
using namespace std;
 
const string Err = "NG";
const string Ok = "OK";
 
enum type {
    LOWER,
    UPPER,
    DIGIT,
    OTHER,
    MAX,
};
 
int main(void)
{
    string s;
 
    while (cin >> s){
        //cout << "s : " << s << endl;
 
        if (s.size() <= 8){
            cout << Err << endl;
            continue;
        }
 
        int State[type::MAX] = {0};
        for (char c : s){
            if (islower(c)){
                State[type::LOWER] += 1;
            }else if (isupper(c)){
                State[type::UPPER] += 1;
            }else if (isdigit(c)){
                State[type::DIGIT] += 1;
            }else{
                State[type::OTHER] += 1;
            }
        }
        int typenum = 0;
        for (int i = 0; i < type::MAX; ++i){
            if (State[i] > 0) typenum += 1;
        }
 
        if (typenum < 3){
            cout << Err << endl;
            continue;
        }
 
        // 判断有没有长度超过 2 的子串
        int max_substr_len = 3;
        string target;
        bool isFound = false;
        for (int i = 0; i < s.size()-max_substr_len; i ++){
            for (int j = i + max_substr_len; j < s.size()-max_substr_len; j ++){
                // 如果字符串相同
                if (0 == s.compare(i, max_substr_len, s, j, max_substr_len)){
                    isFound = true;
                    break;
                }
            }
            if (isFound) break;
        }
        if (isFound)
            cout << Err << endl;
        else
            cout << Ok << endl;
 
    }
 
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

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

创作者周榜

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