Password verification qualified program (C++ Huawei machine test)

Problem-solving ideas:

(1) Judging the length

(2) Statistics of different types of characters

(3) Determine whether the string is repeated every three

#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;
}

 

Guess you like

Origin blog.csdn.net/coolsunxu/article/details/114733333