题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
讲一下最后一个条件,不能含有长度大于2的不含有公共元素的重复子串,这句话意思是含有公共元素的重复子串是OK的,暴力解法的话直接遍历两个for验证长度是3的情况,因为长度更大的是3的子集。
#include <string>
#include <map>
#include <vector>
#include <cctype>
using namespace std;
int main() {
string str, result;
while (cin >> str) {
bool lowalpha = false, upalpha = false, digit = false, other = false;
bool changed = false;
if (str.size() > 8) {
for (int i = 0; i < str.size(); i++) {
if (islower(str[i])) lowalpha = true;
if (isupper(str[i])) upalpha = true;
if (isdigit(str[i])) digit = true;
if (ispunct(str[i])) other = true;
}
if (lowalpha & upalpha & digit & other ||
lowalpha & upalpha & digit || upalpha & digit & other ||
lowalpha & digit & other) {
for(int i =0;i<str.size()-6;i++){
for(int j= i+3;j<str.size()-3;j++){
if((str[i]==str[j])&&(str[i+1]==str[j+1])&&
(str[i+2]==str[j+2])){
result ="NG";changed=true;}
}
}
if(!changed) result = "OK";
}
else result ="NG";
}
else result = "NG";
cout<<result<<endl;
}
}