题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <iostream>
#include <set>
using namespace std;
int main() {
string s;
while(cin >> s){
bool fa = true;
set<string> st;
int letter_a=0, letter_A=0, num=0, other = 0, len = s.size();
if(len <= 8){
cout << "NG" << endl;
continue;
}
for(int i=0; i<len; i++){
if((s[i] >= 'A' && s[i] <= 'Z')){
letter_A = 1;
}
else if((s[i] >= 'a' && s[i] <= 'z')){
letter_a = 1;
}
else if((s[i] >= '0' && s[i] <= '9')){
num = 1;
}
else{
other = 1;
}
if(i >= 2){
string s1 = s.substr(i-2,3);
if(st.find(s1) == st.end()){
st.insert(s1);
}else{
fa = false;
}
}
}
int res = letter_a + letter_A + num + other;
if(fa && res >=3){
cout << "OK" << endl;
}else{
cout << "NG" << endl;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")
注意点:
1 对于重复想到用“集合”这个数据结构进行判断
2 不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
该条件的判断要想到只要3个元素的字串是否重复就可以了
查看18道真题和解析