标签:
AABCD CDAA ASD ASDF
yes no
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100010
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;
char str[2*maxn],pstr[2*maxn];
int nextval[2*maxn];
void get_nextval()
{
int plen=strlen(pstr);
int i=0,j=-1;
nextval[0]=-1;
while (i<plen)
{
if (j==-1||pstr[i]==pstr[j])
{
i++;
j++;
if (pstr[i]!=pstr[j])
nextval[i]=j;
else
nextval[i]=nextval[j];
}
else
j=nextval[j];
}
}
bool KMP()
{
int i=0,j=0;
int len=strlen(str);
int plen=strlen(pstr);
while (i<len&&j<plen)
{
if (j==-1||str[i]==pstr[j])
{
i++;
j++;
}
else
j=nextval[j];
}
if (j==plen)
return true;
return false;
}
int main()
{
while (~scanf("%s%s",str,pstr))
{
int len=strlen(str);
for (int i=0;i<len;i++)
str[len+i]=str[i];
str[len+i]='\0';
get_nextval();
if (KMP())
printf("yes\n");
else
printf("no\n");
}
return 0;
}
/*
AABCD
CDAA
ASD
ASDF
*/
版权声明:本文博客原创文章。博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4710998.html