标签:nyoj327
AABCD CDAA ASD ASDF
yes no
#include <stdio.h>
#include <string.h>
#define maxn 100000 + 5
char str1[2 * maxn], str2[maxn];
int next[maxn], len1, len2;;
void getNext(){
int j = -1, i = 0;
next[0] = -1;
while(i < len2){
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];
}
}
bool KMP(){
getNext();
int i = 0, j = 0;
while(i < len1 && j < len2){
if(j == -1 || str1[i] == str2[j]) ++j, ++i;
else j = next[j];
}
return j == len2;
}
int main(){
while(scanf("%s%s", str1, str2) == 2){
len1 = strlen(str1);
len2 = strlen(str2);
if(len1 < len2){
printf("no\n");
continue;
}
memcpy(str1 + len1, str1, len1);
len1 *= 2;
if(KMP()) printf("yes\n");
else printf("no\n");
}
return 0;
}
NYOJ327 亲和串 【KMP】,布布扣,bubuko.com
标签:nyoj327
原文地址:http://blog.csdn.net/chang_mu/article/details/24889559