标签:
AABCD CDAA ASD ASDF
yes no
#include<stdio.h> #include<string.h> //KMP const int N=100000; char s1[2*N+10],s2[N+10],next[2*N+10]; void get_next(char *s,int pa){ next[0]=-1;next[1]=0; int i=1,j=0; while(i<pa){ if(j==-1||s[j]==s[i]){ ++i,++j; next[i]=j; } else j=next[j]; } } int KMP(char *s,char *a,int ps,int pa,int ns,int am){ int i,j; i=j=0; while(i<ns){ if(j==-1||s[i]==a[j]){ ++i;++j; } else j=next[j]; if(j==am) return i-am+1; } return -1; } int main(){ while(~scanf("%s%s",s1,s2)){ int len=strlen(s1); if(len<strlen(s2)){ printf("no\n"); continue; } for(int i=0,j=len;i<len;++i,++j){ s1[j]=s1[i]; } get_next(s1,2*len); if(KMP(s1,s2,0,0,2*len,strlen(s2))!=-1) printf("yes\n"); else printf("no\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq_18062811/article/details/47090429