Introduction to hdu 2203 affinity string KMP
Affinity stringTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 9065 Accepted Submission (s): 4135
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
Let's talk about the solution. In fact, we only need to copy the s1 string twice in sequence, because no matter how shift, it cannot exceed twice the s1 length, for example, asdfg string, we only need to save it in s3 like this, s3 = asdfgasdfg.
There are two types of code: KMP, and KMP code using the strstr () function in the C language library function:
# Include
# Include
# Define MAX 100100 char str1 [MAX], str2 [MAX], str3 [MAX * 2]; int next [MAX]; void getNext () {int I =-1, j = 0; int len = strlen (str2); next [0] =-1; while (j
KMP code is not required:
#include
#include
#define MAX 100100char str1[MAX] , str2[MAX] , str3[MAX*2];int main(){while(gets(str1)){gets(str2) ;strcpy(str3,str1) ;strcpy(str3+strlen(str1),str1) ;if(strstr(str3,str2))puts(yes) ;elseputs(no) ;}return 0 ;}