You want to type the string ss, consisting of nn lowercase Latin letters, using your favorite text editor Notepad#.
Notepad# supports two kinds of operations:
- append any letter to the end of the string;
- copy a continuous substring of an already typed string and paste this substring to the end of the string.
Can you type string ss in strictly less than nn operations?
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.
The first line of each testcase contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the string ss.
The second line contains a string ss, consisting of nn lowercase Latin letters.
The sum of nn doesn't exceed 2⋅1052⋅105 over all testcases.
Output
For each testcase, print "YES" if you can type string ss in strictly less than nn operations. Otherwise, print "NO".
本题需要从字符串中找到重复的子串,且字串最少要有两个字符。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int n,m,flag;
string s;
int main(){
cin>>n;
while(n--){
cin>>m;
cin>>s;
flag=0;
if(m<4){ //m小于4时,不存在操作数可以小于m的字符串 ,此时直接输出NO;
cout<<"NO"<<endl;
continue;
}
for(int i=0;i<m;i++){ //循环找出可以重复的两个字符;
for(int j=i+2;j<m;j++){
if(s[i]==s[j]&&j+1<m&&s[i+1]==s[j+1]){
cout<<"YES"<<endl; //找到;
flag=1;
break;
}
}if(flag==1) break; //若找到,跳出当前循环;
}
if(flag!=1) cout<<"NO"<<endl;//找不到,输出NO;
}
}