题目链接
分析:主要就是判断字符串内不能有长度超过2的重复字串。
那么就搜索看是不是存在长度为3的重复字串,如果没有就ok。
字符串函数
s
.
s
u
b
s
t
r
(
i
,
l
e
n
)
s.substr(i,len)
s.substr(i,len)熟悉一下,表示从字符串s的下标i处截取长度为len的字串。
#include<iostream>
#include<cstring>
using namespace std;
string s;
int main(){
std::ios::sync_with_stdio(false);
while(cin>>s){
int len=s.length();
if(len<=8) {cout<<"NG"<<endl;}
else{
int ans1=0,ans2=0,ans3=0,ans4=0;
for(int i=0;i<len;i++){
if(s[i]>='a'&&s[i]<='z') {ans1=1;}
else if(s[i]>='A'&&s[i]<='Z') {ans2=1;}
else if(s[i]>='0'&&s[i]<='9') {ans3=1;}
else {ans4=1;}
}
if(ans1+ans2+ans3+ans4<3){cout<<"NG"<<endl;}
else{
//不能拥有长度超过2(>=3)的字串重复
bool book=false;
for(int i=0;i+3<len;i++){
string str1=s.substr(i,3);
for(int j=i+1;j+3<len;j++){
string str2=s.substr(j,3);
if(str1==str2){
book=true;
break;
}
}
}
if(book) {cout<<"NG"<<endl;}
else {cout<<"OK"<<endl;}
}
}
}
return 0;
}