Affinity string
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 8049 accepted submission (s): 3719
Problem description: The more people get older, the smarter they get, and the more stupid they get. This is a question that deserves the attention of 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.
The input question 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
For more information about the Chinese Question, go to the Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 100010#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;char str[2*maxn],pstr[2*maxn];int nextval[2*maxn];void get_nextval(){ int plen=strlen(pstr); int i=0,j=-1; nextval[0]=-1; while (i<plen) { if (j==-1||pstr[i]==pstr[j]) { i++; j++; if (pstr[i]!=pstr[j]) nextval[i]=j; else nextval[i]=nextval[j]; } else j=nextval[j]; }}bool KMP(){ int i=0,j=0; int len=strlen(str); int plen=strlen(pstr); while (i<len&&j<plen) { if (j==-1||str[i]==pstr[j]) { i++; j++; } else j=nextval[j]; } if (j==plen) return true; return false;}int main(){ while (~scanf("%s%s",str,pstr)) { int len=strlen(str); for (int i=0;i<len;i++) str[len+i]=str[i]; str[len+i]='\0'; get_nextval(); if (KMP()) printf("yes\n"); else printf("no\n"); } return 0;}/*AABCDCDAAASDASDF*/
Affinity string (HDU 2203 KMP)