题解 | 密码验证合格程序

密码验证合格程序

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

#include <iostream>
#include <string>
#include <variant>
#include <vector>
using namespace std;

int main() {
    string input;
    
    while (getline(cin, input)) { 
	  //判断长度
        if(input.size() < 8)    
        {
            cout << "NG" << endl;
            continue;
        }
	  
	  //计算字符类型数量
        vector<int> tmp(4,0);
        for(auto c : input)
        {
            if(c >= '0' && c <= '9')
            {
                tmp[0]++;
            }
            else if(c >='A' && c <= 'Z')
            {
                tmp[1]++;
            }
            else if(c > 'a' && c <= 'z')
            {
                tmp[2]++;
            }
            else
            {
                tmp[3]++;
            }
        }
        int class_cnt = 0;
        for(int i = 0; i < 4; ++i)
        {
            if(tmp[i] > 0)
            {
                class_cnt++;
            }
        }
        if(class_cnt < 3)
        {
            cout << "NG" << endl;
            continue;
        }

	  //检查是否有重复字符串
        int len = input.size();
        bool isNg = false;
        for(int i = 0; i <= len - 6; ++i)
        {
            string sub_str = input.substr(i, 3);
            string last_str = input.substr(i+3);
            if(input.find(sub_str, i+3) != string::npos)
            {
                cout << "NG" << endl;
                isNg = true;
                break;
            }

        }
        if(isNg == false)
            cout << "OK" << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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