题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
前面两个要求都很简单,第三个其实也不难,长度大于2字串不能重复那么只需要判断长度为3的字串是否重复即可。看到别人用find找重复字串确实更方便。。。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
while (cin >> str) {
int count[4] = {0};
bool flag = true;
if (str.length() < 8) {
cout << "NG" << endl;
continue;
}
for (int i = 0; i < str.length(); i++) {
if (isdigit(str[i]))
count[0] = 1;
else if (isupper(str[i]))
count[1] = 1;
else if (islower(str[i]))
count[2] = 1;
else
count[3] = 1;
}
if (count[0] + count[1] + count[2] + count[3] < 3) {
cout << "NG" << endl;
continue;
}
//判断长度大于2的字串是否重复只需要判断有三个字符串的字串重复就行了
for (int i = 0; i < str.size() - 3; i++) {
for (int j = i + 1; j < str.size() - 3; j++) {
if (str.substr(i, 3) == str.substr(j, 3)) {
cout << "NG" << endl;
flag = false;
break;
}
}
}
if (flag == true)
cout << "OK" << endl;
}
}