The first two methods are pure KMP.
# Include <stdio. h>
# Include <string. h>
# Include <iostream>
Using namespace STD;
Char str1 [1, 200010], str2 [100010];
Int next [100010];
Int get ()
{
Next [0] =-1;
Int J = 0;
Int K =-1;
Int Len = strlen (str2 );
While (j <len-1)
{
If (k =-1 | str2 [k] = str2 [J])
{
J ++;
K ++;
Next [J] = K;
}
Else K = next [k];
}
Return 0;
}
Int main ()
{
Int I, J;
While (~ Scanf ("% S % s", str1, str2 ))
{
Get ();
Char STR [100010];
Strcpy (STR, str1 );
Strcat (str1, STR );
// Printf ("& \ n ");
Int len1 = strlen (str1 );
Int len2 = strlen (str2 );
Int I = J = 0;
While (I <len1 & J <len2)
{
If (j =-1 | str1 [I] = str2 [J])
{
I ++;
J ++;
}
Else J = next [J];
}
If (j = len2) printf ("Yes \ n ");
Else printf ("NO \ n ");
}
Return 0;
}
The functions in STL are used below.
# Include <stdio. h>
# Include <string. h>
# Include <iostream>
Using namespace STD;
Int main ()
{
Char str1 [200010], str2 [100010], STR [100010];
Int I, J;
While (~ Scanf ("% S % s", str1, str2 ))
{
Strcpy (STR, str1 );
Strcat (str1, STR );
If (strstr (str1, str2) printf ("Yes \ n ");
Else printf ("NO \ n ");
}
Return 0;
}
Hdu2203 KMP questions