题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
输出
OK NG NG OK
代码1:
//第二十题 密码验证合格程序
#include<iostream>
#include<vector>
#include<string>
#include<string.h>
using namespace std;
bool EncodePassword(string test)
{
int i_flag = 0;
size_t i_max = test.length();
if (i_max < 9)//condition 1
{
return false;
}
for (int i = 0; i < i_max; i++)
{
if (isdigit(test.at(i)))
{
i_flag = i_flag | 1;
}
else if (isupper(test.at(i)))
{
i_flag = i_flag | 2;
}
else if (islower(test.at(i)))
{
i_flag = i_flag | 4;
}
else
{
i_flag = i_flag | 8;
}
}
if (i_flag != 7 && i_flag != 11 && i_flag < 13)//condition 2
{
return false;
}
int i_max2 = i_max - 2;
for (int i = 0; i < i_max2; i++)//condition 3
{
for (int j = i + 1; j < i_max2; j++)
{
if (test[i] == test[j] && test[i + 1] == test[j + 1] && test[i+2]== test[j+2])
{
return false;
}
}
}
return true;
}
int main()
{
vector<string>v_instrs;
string in_str;
while (cin>>in_str)
{
v_instrs.push_back(in_str);
}
size_t i_max = v_instrs.size();
for (int i = 0; i < i_max; i++)
{
if (EncodePassword(v_instrs.at(i)))
{
cout << "OK" << endl;
}
else
{
cout << "NG" << endl;
}
}
system("pause");
return 0;
}
在线测试,4ms
代码2:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
while (cin >> str)
{
int flag[4] = { 0 };
if (str.size() <= 8)
goto NG;
for (int i = 0; i < str.size(); ++i)
if (str[i] >= 'a' && str[i] <= 'z')
flag[0] = 1;
else if (str[i] >= 'A' && str[i] <= 'Z')
flag[1] = 1;
else if (str[i] >= '0' && str[i] <= '9')
flag[2] = 1;
else
flag[3] = 1;
if (flag[0] + flag[1] + flag[2] + flag[3] < 3)
goto NG;
for (int i = 0; i <= str.size() - 6; i++)
for (int j = i + 3; j < str.size(); j++)
if (str[i] == str[j] && str[i + 1] == str[j + 1] && str[i + 2] == str[j + 2])
goto NG;
OK:
cout << "OK" << endl; continue;
NG:
cout << "NG" << endl;
}
system("pause");
return 0;
}