#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
//typedef long long lld;
const int oo=0x3f3f3f3f;
//const lld OO=1LL<<61;
const int MOD=(1e9)+7;
const int maxn=100005;
int next[maxn];
char s1[maxn<<1],s2[maxn];
void get_next(int len)
{
int i=0;next[i]=-1;
int j=-1;
while(i<len-1)
{
if(j==-1||s2[i]==s2[j])
{
i++;j++;
next[i]=j;
}
else
j=next[j];
}
}
int kmp(int pos,int len1,int len2)
{
int i=pos;
int j=0;
while(i<len1&&j<len2)
{
if(j==-1||s1[i]==s2[j])
{
i++;
j++;
}
else
j=next[j];
}
if(j>=len2)
return i-len2;
else
return -1;
}
int main()
{
while(scanf("%s%s",s1,s2)!=EOF)
{
int len1=strlen(s1);
int len2=strlen(s2);
if(len2>len1)
{
puts("no");
continue;
}
get_next(len2);
for(int i=0;i<len1;i++)
{
s1[i+len1]=s1[i];
}
s1[2*len1]='\0';
int f=kmp(0,2*len1,len2);
if(f!=-1)
puts("yes");
else
puts("no");
}
return 0;
}
/**
abcd
-> abcdabc
abcda
*/
hdu 2203 亲和串 (kmp入门)
最新推荐文章于 2019-05-19 23:04:10 发布