题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <string>
#include <cctype>
#include <unordered_map>
using namespace std;
bool check_fun1(const string input_str) {
bool uppercase = false;
bool lowercase = false;
bool digit = false;
bool other = false;
for (auto c : input_str) {
if (isupper(c)) {
uppercase = true;
} else if (islower(c)) {
lowercase = true;
} else if (isdigit(c)) {
digit = true;
} else {
other = true;
}
}
return ((uppercase && lowercase && digit)||
(uppercase && lowercase && other) ||
(uppercase && digit && other) ||
(lowercase && digit && other));
}
bool check_substr(const string& input_str) {
int n = input_str.size();
unordered_map<string, int> substr_count;
for (int len = 3;len < n; len++) {
for (int i = 0; i <= n - len; i++) {
string sub = input_str.substr(i, len);
substr_count[sub]++;
if (substr_count[sub] > 1) {
return true;
}
}
}
return false;
}
int main() {
string input;
while (getline(cin, input)) {
if(input.size() < 8) {
cout << "NG" << endl;
} else if (check_fun1(input) == false) {
cout << "NG" << endl;
} else if (check_substr(input)) {
cout << "NG" << endl;
} else {
cout << "OK" << endl;
}
}
return 0;
}
查看9道真题和解析