http://acm.hdu.edu.cn/showproblem.php?pid=2203
也就是判断模式串能否在目标串复制一倍后被包含,转化一下再利用kmp
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=100000+1000;
char T[MAXN*2],P[MAXN];
int n,m,next[MAXN];
void getFail()
{
next[0]=next[1]=0;
for(int i=1;i<m;i++)
{
int j=next[i];
while(j && P[i]!=P[j]) j=next[j];
next[i+1] = (P[i]==P[j])?j+1:0;
}
}
bool KMP()
{
if(n<m) return false;
int j=0;
for(int i=0;i<n*2;i++)
{
while(j && T[i]!=P[j]) j=next[j];
if(T[i]==P[j]) j++;
if(j==m) return true;
}
return false;
}
int main()
{
while(scanf("%s%s",T,P)==2)
{
n=strlen(T);
m=strlen(P);
for(int i=n;i<2*n;i++)
T[i]=T[i-n];//把它复制成二倍,再判断是否可以匹配
T[2*n]=0;
if(KMP()) printf("yes\n");
else printf("no\n");
}
return 0;
}