Hidden String
Problem Description
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.
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
#include<cstdio> #include<cstring> using namespace std; struct str { char s1[15]; char s2[15]; char s3[15]; }S[200]; bool check(char *s,str a) { char *p; char *p2=s; char *p1=strstr(s,a.s1); if(p1) { for(p=p2;p<=p1+strlen(a.s1)-1;p++) *p='#'; p2=p1; p1=strstr(s,a.s2); if(p1) { for(p=p2;p<=p1+strlen(a.s2)-1;p++) *p='#'; p2=p1; p1=strstr(s,a.s3); if(p1) return true; } } return false; } int main() { int i,j,k,o,t; char p[]="anniversary"; char s[105]; char temp[105]; int cnt=0; for(i=0;i<9;i++) for(j=i+1;j<10;j++) { for(o=t=0;t<=i;t++) S[cnt].s1[o++]=p[t]; for(o=0;t<=j;t++) S[cnt].s2[o++]=p[t]; for(o=0;t<11;t++) S[cnt].s3[o++]=p[t]; cnt++; } scanf("%d",&t); while(t--) { scanf("%s",s); for(i=0;i<cnt;i++) { strcpy(temp,s); bool flag=check(temp,S[i]); if(flag)break; } if(i!=cnt) printf("YES "); else printf("NO "); } return 0; }