Affinity string time limit: 1000 MS | memory limit: 65535 KB difficulty: 3
-
Description
-
Recently, zyc encountered a very difficult problem: Judging the affinity string can be seen directly when judging the affinity string before, but now it is different. Now the two strings are very large, the zyc header is dizzy. So zyc hopes that you can help him find a way to quickly judge the affinity string. Affinity string definition: Given two strings s1 and s2, if s2 can be contained in s1 by repeating the movement through s1, then s2 is the affinity string of s1.
-
Input
-
This topic contains multiple groups of test data. The first line of each group contains the input string s1, the second line contains the input string s2, And the s1 and s2 length are less than 100000.
-
Output
-
If s2 is an s1 affinity string, "yes" is output; otherwise, "no" is output ". The output of each group of tests occupies one row.
-
Sample Input
-
AABCDCDAAASDASDF
-
Sample output
-
yesno
#include
#include
#define maxn 100000 + 5char str1[2 * maxn], str2[maxn];int next[maxn], len1, len2;;void getNext(){int j = -1, i = 0;next[0] = -1;while(i < len2){if(j == -1 || str2[i] == str2[j]){++i; ++j;if(str2[i] != str2[j]) next[i] = j;else next[i] = next[j];}else j = next[j];}}bool KMP(){getNext();int i = 0, j = 0;while(i < len1 && j < len2){if(j == -1 || str1[i] == str2[j]) ++j, ++i;else j = next[j];}return j == len2;}int main(){while(scanf("%s%s", str1, str2) == 2){len1 = strlen(str1);len2 = strlen(str2);if(len1 < len2){printf("no\n");continue;}memcpy(str1 + len1, str1, len1);len1 *= 2;if(KMP()) printf("yes\n");else printf("no\n");}return 0;}