先把文本串复制一遍再扫kmp
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
#define maxn 220000
int next[maxn],n,m;
char st[maxn],st2[maxn];
void getnext()
{
next[0]=0;
for(int i=1;i<n;i++)
{
int j=next[i];
while(j&&st2[i]!=st2[j])
{
j=next[j];
}
if(st2[j]==st2[i])
{
next[i+1]=j+1;
}
else
next[i+1]=0;
}
}
void cal()
{
int j=0;
for(int i=0;i<m;i++)
{
while(j&&st[i]!=st2[j])
{
j=next[j];
}
if(st[i]==st2[j])
{
j++;
}
if(j==n)
{
printf("yes\n");
return;
}
}
printf("no\n");
}
int main()
{
while(scanf("%s",st)!=EOF)
{
m=strlen(st);
for(int i=m;i<2*m;i++)
{
st[i]=st[i-m];
}
m=m*2;
scanf("%s",st2);
n=strlen(st2);
getnext();
cal();
}
return 0;
}