密码验证合格程序

来源:互联网 发布:芒果tv网络电视会员 编辑:程序博客网 时间:2024/05/22 00:11

描述

密码要求:

 

 

 

1.长度超过8位

 

 

 

2.包括大小写字母.数字.其它符号,以上四种至少三种

 

 

 

3.不能有相同长度超2的子串重复

 

 

 

说明:长度超过2的子串


知识点字符串,数组运行时间限制0M内存限制0输入

一组或多组长度超过2的子符串。每组占一行

输出

如果符合要求输出:OK,否则输出NG

 

每行输出对应一组输入的结果;


样例输入021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000样例输出OK NG NG OK二、解法
<pre name="code" class="cpp">#include <iostream>#include <string>#include <vector>//#include <regex>#include <algorithm>using namespace std;//KMP方法中的next表void getNext(string needle,vector<int> &next){next[0]=-1;int i=0,k=-1,size=needle.size();while(i<size-1){if(k==-1 || needle[i]==needle[k]){if(needle[++i]==needle[++k])next[i]=next[k];elsenext[i]=k;}else{k=next[k];}}}//求最大公共字符长度int strStr(string haystack, string needle) {if(haystack.empty()||needle.empty())return 0;int i=0,j=0,hSize=haystack.size(),nSize=needle.size(),max=0;vector<int> next(nSize,0);//获取next表getNext(needle,next);//求最大公共字符串长度while(i<hSize&&j<nSize&&max<3){if(j==-1||haystack[i]==needle[j]){++i,++j;max=max<j?j:max;//将每次求出的计算结果的最大值保存在max中}elsej=next[j];}return max;}//获取两个字符串的最大长度int getCommonStrLength(string haystack, string needle){//若为空,则返回if(haystack.empty()||needle.empty())return 0;int i,size=needle.size(),max=0,tmp;string str;for(i=0;i<size;i++){//避免从中间开始匹配麻烦,保证字符匹配是从头开始匹配的str=needle.substr(i);tmp=strStr(haystack,str);max=max<tmp?tmp:max;}return max;}//判断是否含有大于2的重复字串bool getRepeat(string str){vector<string> vec;int i,size=str.size();for(i=2;i<size-2;i++){if(getCommonStrLength(str.substr(0,i),str.substr(i))>2)return true;}return false;}int main(){string str;int count=0;//regex cap("[A-Z]+"),low("[a-z]+"),digital("[0-9]+"),other("[^A-Za-z0-9]");while(getline(cin,str)){//判断是否大于8if(str.size()<=8){cout<<"NG"<<endl;continue;}//判断是否含有大写、小写、数字和其他字符中任意三种int i,size=str.size(),a=0,b=0,c=0,d=0;for(i=0;i<size;i++){if(str[i]>='a'&&str[i]<='z')b=1;else if(str[i]>='A'&&str[i]<='Z')c=1;else if(str[i]>='0'&&str[i]<='9')a=1;elsed=1;}if(a+b+c+d<3){cout<<"NG"<<endl;continue;}//判断是否含大于2的字串if(getRepeat(str))cout<<"NG"<<endl;elsecout<<"OK"<<endl;}system("pause");return 0;}


0 0