描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1≤n≤100
输入描述:
一组字符串。
输出描述:
如果符合要求输出:OK,否则输出NG
示例
输入:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出:
OK
NG
NG
OK
代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> str_array;
while (1) {//多行输入字符串
string a;
getline(cin, a);
if (a != "")
str_array.push_back(a);
else
break;
}
for (int i = 0; i < str_array.size(); i++) { //遍历输入字符串
int types[4] = { 0, 0, 0, 0 };//字符串输入类别标记
if (str_array[i].size() < 8)//字符串长度小于8的不符
cout << "NG" << endl;
else {
string b = str_array[i];
for (int j = 0; j < b.size(); j++) {//遍历字符串字母 判断类别
if (b[j] >= 'a' && b[j] <= 'z') {
types[0] = 1;
} else if (b[j] >= 'A' && b[j] <= 'Z') {
types[1] = 1;
} else if (b[j] >= '0' && b[j] <= '9') {
types[2] = 1;
} else {
types[3] = 1;
}
}
int sum = 0;//类别总数
for (int k = 0; k < 4; k++) {
sum += types[k];
}
if (sum < 3) {//类别小于3种的不符
cout << "NG" << endl;
} else {
int len = b.size();
bool isright = true;
for (int k = 0; k < len - 6; k++) {//遍历3字符的子字符串
string c = b.substr(k, 3);
for (int m = k + 3; m < len - 3; m++) {//搜寻后续子字符串
string d = b.substr(m, 3);
if (c == d) {//检查是否有相同的3字符子字符串
isright = false;
break;
}
}
}
if (isright) {
cout << "OK" << endl;
} else {
cout << "NG" << endl;
}
}
}
}
}
结果