Affinity String
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5641 accepted Submission (s): 2546
Problem Description people as they grow older is more and more clever or more stupid, this is a question worthy of scientists around the world, the same problem Eddy has been thinking, because he was very young to know how the affinity string to judge, but found that Now grow up and do not know how to judge affinity string, so he had to ask the smart and helpful to help you solve this problem.
The affinity string is defined as this: given the two strings S1 and S2, if the S2 is included in the S1 by the S1 loop shift, then we say that S2 is the affinity string for S1.
Input there are several sets of test data, the first row of each group of data contains the input string s1, the second row contains the input string s2,s1 and s2 length are less than 100000.
Output "Yes" if S2 is a S1 affinity string, whereas output "no". The output for each set of tests is one row.
Sample Input
AABCD Cdaa ASD asdf
Sample Output
Yes No
Author Eddy
Recommend LCY
KMP, direct search is also available.
#include <cstdio>
#include <cstring>
#define MAX 100010
void GetNext (char s[],int next[]) {
int i,j,length;
Next[0]=-1;
i=0;
J=-1;
Length=strlen (s);
while (i<length) {
if (j==-1| | S[i]==s[j]) {
i++;j++;next[i]=j;
}
else j=next[j];
}
int KMP (char t[],char p[],int next[]) {
int i=0,j=0;
int Len1=strlen (t), Len2=strlen (p);
while (I<LEN1&&J<LEN2) {
if (j==-1| | T[I]==P[J])
i++,j++;
else j=next[j];
}
if (J==LEN2) return i-len2;
else return-1;
}
int main () {
char S1[max*2],s2[max];
int L1,l2,next[max];
while (~SCANF ("%s", S1)) {
scanf ("%s", S2);
L1=strlen (S1);
for (int i=0;i<l1;i++)
s1[i+l1]=s1[i];
s1[l1*2]=0;
GetNext (s2,next);
printf ("%s\n", KMP (s1,s2,next) ==-1? " No ": yes");
}