Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a string s of length n. He wants to find three nonoverlapping substrings s[l1..r1], s[l2..r2], s[l3..r3] that:
1. 1≤l1≤r1<l2≤r2<l3≤r3≤n
2. The concatenation of s[l1..r1], s[l2..r2], s[l3..r3] is "anniversary".
Input
There are multiple test cases. The first line of input contains an integer
T (1≤T≤100), indicating the number of test cases. For each test case:
There's a line containing a string s (1≤|s|≤100) consisting of lowercase English letters.
There's a line containing a string s (1≤|s|≤100) consisting of lowercase English letters.
Output
For each test case, output "YES" (without the quotes) if Soda can find such thress substrings, otherwise output "NO" (without the quotes).
Sample Input
2 annivddfdersewwefary nniversarya
Sample Output
YES NO
Source
Recommend
/*
先求出前缀和后缀匹配,注意anniversaxxrxxy这组数据。。被卡了一上午,,因为访问到第二个a的时候会让前缀变成1
然后就for循环找中间匹配的串,保证这个串的第一个和最后一个与头尾相连
比赛的时候狗血代码都过了- -。。。如果自己hack自己涨分的话......
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
char s1[110];
char s2[110] = "anniversary";
int pre1[110], pre2[110];
int T;
int main()
{
scanf("%d", &T);
while(T--){
scanf("%s", s1);
int len1 = strlen(s1);
int len2 = strlen(s2);
memset(pre1, 0, sizeof(pre1));
memset(pre2, 0 ,sizeof(pre2));
int cout = 0;
for(int i = 0 ; i < len1; i++){
if(s1[i] == 'a' && cout == 0){
pre1[i] = 1;
cout = 1;
}
else if(s1[i] == s2[cout]){
pre1[i] = pre1[i-1] + 1;
cout++;
}
else {
pre1[i] = 0;
cout= 0;
}
}
// for(int i = 0; i < len1; i++)
// printf("%d ",pre1[i]);
cout = 0;
for(int i = len1 - 1; i >= 0 ; i--){
if(s1[i] == 'y'){
pre2[i] = 1;
cout = 1;
}
else if(s1[i] == s2[len2 - cout-1]){
pre2[i] = pre2[i+1] + 1;
cout++;
}
else {
pre2[i] = 0;
cout = 0;
}
}
// for(int i = 0 ; i < len1; i++)
// printf("%d ",pre2[i]);
int flag = 0;
cout = 0;
for(int i = 0 ; i < len1 ; i++){
for(int j = i + 1; j < len1; j++){
cout = 0;
for(int k = i + 1; k < j; k++){
if(s1[k] == s2[pre1[i]+cout]){
cout++;
}
else {
cout = 0;
if(s1[k] == s2[pre1[i]+cout])
cout++;
}
if(cout + pre1[i] + pre2[j] == len2 && s1[k] == s2[len2 - pre2[j] - 1]){
flag = 1;
break;
}
}
if(flag) break;
}
if(flag) break;
}
// for(int i = 0 ; i < len1; i++)
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}