[Topic]
Determine whether the two ring strings are the same.
[Idea]
Copy the matching string to the end and use the pattern string as KMP. If it can be found, it indicates the same string.
I haven't written KMP for a long time, and I forgot about the algorithm ..
Little tragedy ~
[CODE]
#include<iostream>#include<cstdio>using namespace std;char t[222],s[444];int next[222];void setNext(){ next[0]=-1; int j=0,k=-1; while( j<strlen(t) ) { if( k==-1||t[j]==t[k] ) next[++j]=++k; else k=next[k]; }}bool judge(){ int l1=strlen(s),l2=strlen(t); int i=0,j=0,k; while( i<l1&&j<l2 ) { if( j==-1||s[i]==t[j] ) i++,j++; else j=next[j]; } return j==l2;}int main(){ while( scanf("%s",&t)!=EOF ) { setNext(); int sum=0,n; scanf( "%d",&n ); while( n-- ) { memset( s,0,sizeof(s) ); scanf( "%s",&s ); if( strlen(t)!=strlen(s) ) continue; int len=strlen(s); for( int i=0;i<len;i++ ) s[len+i]=s[i]; s[len<<1]=0; if( judge() ) sum++; } printf( "%d\n",sum ); } return 0;}