标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12533 Accepted Submission(s): 5667
#include<iostream> #include<cstdio> #include<cstring> #include<stdlib.h> #include<algorithm> #include<cmath> #include<string> using namespace std; char a[200005],b[100005]; int Next[100005]; void getNext() { memset(Next,-1,sizeof(Next)); //!!!下标从0开始和从1开始,初始化数组不同,在这TLE了好久 int m=strlen(b),k=-1; for(int i=1; i<m; i++) { while(k>-1&&b[k+1]!=b[i]) k=Next[k]; if(b[k+1]==b[i]) k++; Next[i]=k; } } void CMP() { getNext(); int n=strlen(a),m=strlen(b),k=-1; for(int i=0; i<n; i++) a[i+n]=a[i]; a[2*n]=‘\0‘; n<<=1; for(int i=1; i<n; i++) { while(k>-1&&b[k+1]!=a[i]) k=Next[k]; if(b[k+1]==a[i]) k++; if(k==m-1) { puts("yes"); return; } } puts("no"); } int main() { while(scanf("%s%s",a,b)!=EOF) { CMP(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5905740.html