描述 | |
---|---|
知识点 | 字符串,数组 |
运行时间限制 | 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];
else
next[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中
}
else
j=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))
{
//判断是否大于8
if(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;
else
d=1;
}
if(a+b+c+d<3)
{
cout<<"NG"<<endl;
continue;
}
//判断是否含大于2的字串
if(getRepeat(str))
cout<<"NG"<<endl;
else
cout<<"OK"<<endl;
}
system("pause");
return 0;
}