cf B. Notepad# 题解

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; 
    }    
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值