题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include<iostream>
#include <set>
using namespace std;
// 判断密码的长度
bool check1(string &password){
bool bisValid = true;
if(password.length() < 9){
bisValid = false;
}
return bisValid;
}
//判断密码包含的字符类型
bool check2(string &password){
bool bisValid = true;
int type[4] = {0};
int len = password.length();
for(int i = 0; i < len; i++){
if(password[i] >= 'a' && password[i] <= 'z'){
type[0] = 1;
}else if(password[i] >= 'A' && password[i] <= 'Z'){
type[1] = 1;
}else if(password[i] >= '0' && password[i] <= '9'){
type[2] = 1;
}else{
type[3] = 1;
}
}
if(type[0] + type[1] + type[2] + type[3] < 3){
bisValid = false;
}
return bisValid;
}
// 检查是否有长度大于2的包含公共元素的子串重复
bool check3(string &password){
bool bisValid = true;
set<string> myset;
int len = password.length();
string substr;
for(int i = 0; i < len - 2; i++){
substr = password.substr(i, 3);
if(myset.find(substr) != myset.end()){
bisValid = false;
break;
}
myset.insert(substr);
}
return bisValid;
}
int main(){
string password;
while(getline(cin, password)){
if(check1(password) && check2(password) && check3(password)){
cout << "OK" << endl;
}else{
cout << "NG" << endl;
}
}
return 0;
}

