题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <cctype>
#include <iostream>
#include <random>
#include <string>
#include <unordered_map>
using namespace std;
string check(string s){
if (s.size() <= 8) return "NG"; //1.密码长度
//2.字符种类
bool h_low = false, h_up = false, h_di = false, h_oth = false;
for (char c : s) {
if (islower(c)) h_low = true;
else if (isupper(c)) h_up = true;
else if (isdigit(c)) h_di = true;
else h_oth = true;
}
if((h_low + h_up + h_di + h_oth) < 3) return "NG";
//3.长度大于2的重复字符串
string subStr;
unordered_map<string, int> hash; //用一个哈希表存储所有长度为3的字符串
unordered_map<string, int>::iterator it;
for (char c : s){
if (subStr.size() < 3){ //字符串长度小于3的时候继续加字符串
subStr += c;
}
if (subStr.size() >= 3){ //字符串长度大于等于3的时候滚动窗口,并加入哈希表中
hash[subStr]++;
subStr.erase(0, 1);
subStr += c;
}
}
for (it = hash.begin(); it != hash.end(); it ++){
if (it->second >= 2) return "NG"; //寻找是否有重复的长度>=3的子串
}
return "OK";
}
int main() {
string s;
while(cin >> s) {
string res = check(s);
cout << res << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")

