Description
As the age increases, people become smarter and more stupid. This is a question worthy of thinking from scientists around the world. Eddy has been thinking about the same problem, when he was very young, he knew how to judge the affinity string. But he found that when he was growing up, he did not know how to judge the affinity string, so he had to ask you again to solve the problem with a smart and helpful person.
The affinity string is defined as follows: Given two strings S1 and S2, if S2 can be contained in S1 through S1 cyclic shift, 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: process text strings twice#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 200005;char text[maxn], pattern[maxn];int next[maxn];void getNext() {int m = strlen(pattern);next[0] = 0, next[1] = 0;for (int i = 1; i < m; i++) {int j = next[i];while (j && pattern[i] != pattern[j])j = next[j];next[i+1] = pattern[i] == pattern[j] ? j+1 : 0;}}int kmp() {int n = strlen(text), m = strlen(pattern);int j = 0;for (int i = 0; i < n; i++) {while (j && text[i] != pattern[j])j = next[j];if (text[i] == pattern[j])j++;if (j == m)return true;}return false;}int main() {while (scanf("%s%s", text, pattern) != EOF) {char tmp[2*maxn];memcpy(tmp, text, sizeof(text));strcat(text, tmp);getNext();if (kmp())printf("yes\n");else printf("no\n");}return 0;}
HDU-2203 affinity string