Affinity stringTime
limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9460 Accepted Submission (s): 4320
Problem description People with age growth is the bigger the wiser or the bigger the more stupid, this is a question that deserves the world's scientists to think about, the same question Eddy has been thinking, because he knew in a very small time how affinity string judgment, but found that Now grow up and do not know how to judge the affinity string, so he had to again to ask smart and helpful you to solve the problem.
The definition of affinity string is this: given two strings S1 and S2, if the S2 can be included in S1 by S1 cycle, then we say S2 is the affinity string of S1.
Input has multiple sets of test data, the first row of each group of data contains the input string s1, the second line contains the input string s2,s1 and s2 length are less than 100000.
Output if S2 is the affinity string of the S1, outputs "yes" and, conversely, outputs "no". The output for each group of tests is one row.
Sample Input
Aabcdcdaaasdasdf
Sample Output
Yesno
Authoreddy
Recommendlcy | We have carefully selected several similar problems for you:1358 3336 3746 3068 2202
before doing this problem did not learn KMP, directly write the string matching function, this time with KMP do again
#include <stdio.h> #include <string.h>//kmpconst 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdoj-2203-affinity string "KMP"