思路:
先统计大小写,数字,其他出现的情况,接着判断长度,最后判断是否有重复子串。
子串的统计,复杂度为O(N2);
题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度大于2的子串重复
输入描述:
一组或多组长度超过2的字符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出
OK
NG
NG
OK
Example:
#include<iostream>
using namespace std;
enum {UPPER, LOWER, NUMBER, OTHERS};
int main()
{
char password[256];
int type[4];
while(cin.getline(password, 256)) {
bool OK = true;
for(int i = 0; i < 4; i++) type[i] = 0;
int len = 0;
char ch = password[len];
while(ch != '\0') {
if(isdigit(ch)) {
type[NUMBER] = 1;
} else if(isupper(ch)) {
type[UPPER] = 1;
} else if(islower(ch)) {
type[LOWER] = 1;
} else {
type[OTHERS] = 1;
}
ch = password[++len];
}
if(len <= 8) OK = false;
else {
int count = 0;
for(int i = 0; i < 4; i++) {
count += type[i];
}
if(count < 3) OK = false;
else {
int match = 0;
for(int i = 0; i < len; i++) {
for(int j = i + 1; j < len; j++) {
int tmp = i;
match = 0;
while(j < len && password[tmp] == password[j]) {
tmp++, j++, match++;
}
if(match > 2) {
OK = false;
break;
}
}
if(!OK) break;
}
}
}
cout << (OK ? "OK\n" : "NG\n");
}
}