Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=2203
Main topic:
The definition of a given affinity string: given two strings S1 and S2, if the S2 can be included in the S1 by S1 cycle Shift,
Then we'll say that S2 is the affinity string of S1. Now give you two strings S1 and S2 to determine if S2 is S1 affinity string.
Ideas:
Determine if the string length of the S2 is less than or equal to the length of S1, because if S2 string length is longer than S1, S2 is not
may be the affinity string of S1. Then in the S1 string behind in the street S1 the string, to S1 and S2 KMP algorithm, see S1 string
Whether the string containing S2, if included, then S2 is s1 affinity string, otherwise it is not affinity string.
AC Code:
# include<stdio.h># Include<string.h>char a[200010],b[100010];int next[100010];void getnext (char *pat) { int len_pat = strlen (PAT); int i = 0; Int j =-1; Next[0] =-1; while (I < Len_pat) {if (j = =-1 | | pat[i] = = Pat[j]) {i++; j + +; Next[i] = j; } else J = next[j]; }}int KMP (Char *str, char *pat) {int i = 0; int j = 0; int len_str = strlen (str); int len_pat = strlen (PAT); GetNext (PAT); while (I < LEN_STR) {if (j = =-1 | | str[i] = = Pat[j]) {i++; j + +; } else J = next[j]; if (J >= Len_pat) return 1; } return 0;} int main () {int len_a,len_b,i; while (~SCANF ("%s%s", A, B)) {len_a = strlen (a); Len_b = strlen (b); if (Len_a < Len_b) {printf ("no\n"); Continue } for (i = 0; I <len_a; i++) a[i+leN_A] = A[i]; A[i+len_a] = ' + '; if (! KMP (A, B)) printf ("no\n"); else printf ("yes\n"); } return 0;}
HDU2203 affinity string "KMP"