把anniversary分成三个区间,分别枚举每个区间在给定模板中的长度。每次枚举完一个区间,记录下区间长度和起始坐标,下次从剩下长度开始枚举,避免重复。
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 const int MAXD = 50010; 8 const int MAXM = 200010; 9 const int INF = 0x3f3f3f3f; 10 typedef long long ll; 11 12 int main() 13 { 14 //freopen("in.txt","r",stdin); 15 int T; 16 scanf("%d",&T); 17 char str[100]; 18 char *str2; 19 while(T--) 20 { 21 int flag=0; 22 char a1[100]; 23 char a2[100]; 24 char a3[100]; 25 int len1; 26 int len2; 27 int len3; 28 char *b1,*b2,*b3; 29 scanf("%s",str); 30 str2="anniversary"; 31 int len=strlen(str2); 32 for(int i=0; i<len-2; i++) 33 { 34 if(flag) 35 break; 36 for(int j=i+1; j<len-1; j++) 37 { 38 memset(a1,0,sizeof(a1)); 39 memset(a2,0,sizeof(a2)); 40 memset(a3,0,sizeof(a3)); 41 strncpy(a1,str2+0,i+1); 42 b1=strstr(str,a1); 43 if(b1==NULL) 44 continue; 45 len1=b1-str; 46 strncpy(a2,str2+i+1,j-i); 47 b2=strstr(str+len1+i+1,a2); 48 if(b2==NULL) 49 continue; 50 len2=b2-str; 51 strncpy(a3,str2+j+1,len-j+1); 52 b3=strstr(str+len2+j-i,a3); 53 if(b3==NULL) 54 continue; 55 flag=1; 56 break; 57 } 58 } 59 if(flag) 60 cout<<"YES"<<endl; 61 else 62 cout<<"NO"<<endl; 63 } 64 return 0; 65 }