HDU 2203 affinity string (kmp), hdu2203
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
AuthorEddy
Is to double the first string and then whether it can contain the first string
Kmp Template
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<queue>#include<vector>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)using namespace std;#define N 200005char a[N],b[N];int next[N];void getfail(char *b){int i,j;i=0,j=-1;next[0]=-1;int len=strlen(b);while(i<len){if(j==-1||b[i]==b[j]){i++;j++;next[i]=j;} elsej=next[j];}}int kmp(char *a,char *b){int i,j;int lena=strlen(a);int lenb=strlen(b);i=j=0;while(i<lena){if(j==-1||a[i]==b[j]){i++;j++;} elsej=next[j];if(j==lenb)return 1;} return 0;}int main(){ int i,j; while(~scanf("%s%s",a,b)) { int len=strlen(a); for(i=0;i<len;i++)a[len+i]=a[i]; a[i+len]='\0'; getfail(b); if(kmp(a,b))printf("yes\n"); else printf("no\n"); } return 0;}
ACM hdu 1711 (KMP), the result is displayed, how can the AC fail?
# Include <iostream>
Using namespace std;
Int m, n, str1 [1000005], str2 [10005], next [10005];
Void getnext (int str2 [])
{
Int I = 0, j =-1;
Next [0] =-1;
While (I <m-1)
{
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];
}
}
Int kmp (int str1 [], int str2 [], int pos)
{
Int I = pos, j = 0;
While (I <n & j <m)
{
If (j =-1 | str1 [I] = str2 [j])
{
I ++;
J ++;
}
Else
J = next [j];
}
If (j> = m)
Return I-m + 1;
Else
Return-1;
}
Int main ()
{
Int t, I;
Cin> t;
While (t --)
{
// If (t = 0) you miss the last group of input
// Return 0;
Cin> n> m;
For (I = 0; I <n; I ++)
Cin> str1 [I];
For (I = 0; I <m; I ++)
Cin> str2 [I];
Getnext (str2 );
Cout <kmp (str1, str2, 0) <endl;
}
Return 0;
}
Application of hdu 2594 KMP
Next indicates the position in the search string to continue the comparison after the match fails.
# Include <iostream>
# Include <string. h>
Using namespace std;
Char S1 [50001];
Int next [50000];
Char S2 [100001];
Int len1;
Int len2;
Int ans;
Void build_next ()
{
Int len = strlen (S1 );
Int pos = 2;
Int cnd = 0;
Next [0] =-1;
Next [1] = 0;
While (pos <len)
{
If (S1 [pos-1] = S1 [cnd])
{
Cnd ++;
Next [pos] = cnd;
Pos ++;
}
Else if (cnd> 0)
Cnd = next [cnd];
Else
{
Next [pos] = 0;
Pos ++;
}
}
}
Void KMP ()
{
Int m = 0;
Int I = 0;
If (len2-len1> 1)
M = len2-len1-1;
While (m <len2-1)
{
If (S1 [I] = S2 [m + I])
{
If (I = len1-1)
{
Ans = len1;
Return;
}
Else
{
I ++;
}
}
Else if (m + I = len2-1)
{
Ans = I;
Return;
}
Else
{
M = m + I-next [I];
If (next [I]>-1)
I = next [I];
Else
I = 0;
}
}
}
Int main ()
{
While (gets (S1 ))
{
Gets (S2 );
Len1 = strlen (S1 );
Len2 = strlen (S2 );
S2 [len2 ++] = '*';
Ans = 0;
Build_next ();
KMP ();
If (ans = 0)
{
Cout <0 <endl;
}
Else
{
For (int I = 0; I <ans; I ++)
Cout <S1 [I];
Cout <"" <ans <endl;
}
}
}... Remaining full text>