密码验证合格程序(C++华为机试)

解题思路:

(1)判断长度

(2)统计不同类字符种类

(3)每三个判断是否重复字符串

#include<iostream>
#include<unordered_map>

using namespace std;

bool helper(string str) {
    unordered_map<string,int> mp;
    int a=0,b=0,c=0,d=0;
    for(int i=0;i<str.length();i++) {
        if(i<=str.length()-3) mp[str.substr(i,3)]++;
        if('a'<=str[i] && str[i]<='z') a=1;
        else if('A'<=str[i] && str[i]<='Z') b=1;
        else if('0'<=str[i] && str[i]<='9') c=1;
        else d=1;
    }
    if(a+b+c+d<3) return false;
    else {
        for(auto it=mp.begin();it!=mp.end();it++) {
            if(it->second>1) return false;
        }
        return true;
    }
}

int main() {
    string str="";
    while(cin>>str) {
        if(str.length()<=8) cout<<"NG"<<endl;
        else if(helper(str)) cout<<"OK"<<endl;
        else cout<<"NG"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/114733333
今日推荐