Description:
题目大意:给定一个长度为 n 的字符串,q 次询问,每次询问在字符串中是否存在着s[l,…r]相同的子串,要求字串不连续且在原串的出现次序未发生改变。
解题思路:
算法标签:贪心
假设如果在 s[l] 之前未出现与 s[l] 相同的字符,同时在 s[r] 之后也未出现与它相同的字符,那么只有它自己与它相等(废话),与题意矛盾。所以只有在 l 之前便出现与 s[l] 相同的字符,或者在 r 之后出现与 s[r] 相同的字符才会出现 good 字串。
代码:
// TSWorld
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1005;
int main()
{
int T = 0,l = 0,r = 0,n = 0,q = 0;
string s;
cin>>T;
while(T--) {
cin>>n>>q;
cin>>s;
for(int i = 1;i <= q;i++) {
cin>>l>>r;
l--;
r--;
bool isresult = false;
for(int j = 0;j < l;j++) {
if(s[j] == s[l]) {
isresult = true;
break;
}
}
for(int j = r+1;j < n;j++) {
if(s[j] == s[r]) {
isresult = true;
break;
}
}
if(isresult)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}