密码验证合格程序
题目描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
输入例子:
021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000
输出例子:
OK
NG
NG
OK
解答代码:
#include<iostream>
#include<vector>
#include<iostream>
#include<string>
#include<set>
#include<sstream>
#include<algorithm>
using namespace std;
//判断是否存在长度大于2的子串
int judgeSubStr(string str)
{
int i,j;
string subStr1="";
string subStr2="";
for(i=0; i<str.length()-3; i++)
{
subStr1=str.substr(i,3);
for(j=i+3; j<str.length()-2; j++)
{
subStr2=str.substr(j,3);
if(subStr1==subStr2)
{
return 1;
}
}
}
return 0;
}
int main()
{
string s1;
int i,j,k;
int count=0;
set<char> letterSmall;//存储小写字母
set<char> letterBig;//存储大写字母
set<char> number;//存储数字
set<char> other;//存储其他字符
while(cin>>s1)
{
//先清空容器
letterSmall.clear();
letterBig.clear();
number.clear();
other.clear();
if(s1.length()<=8)
{
//cout<<"length"<<endl;
cout<<"NG"<<endl;
continue;
}
if(judgeSubStr(s1))
{
//cout<<"sub"<<endl;
cout<<"NG"<<endl;
continue;
}
for(i=0; i<s1.length(); i++)
{
if(s1[i]>='a'&&s1[i]<='z')
letterSmall.insert(s1[i]);
else if(s1[i]>='A'&&s1[i]<='Z')
letterBig.insert(s1[i]);
else if(s1[i]>='0'&&s1[i]<='9')
number.insert(s1[i]);
else
other.insert(s1[i]);
}
int count=0;
if(letterSmall.size()>0)//说明存在小写字母
count++;
if(letterBig.size()>0)//说明存在大写字母
count++;
if(number.size()>0)//说明存在数字
count++;
if(other.size()>0)//说明含有其他字符
count++;
//cout<<letterSmall.size()<<letterBig.size()<<number.size()<<other.size()<<endl;
if(count>=3)
{
cout<<"OK"<<endl;
}
else
{
cout<<"NG"<<endl;
}
}
}