http://acm.hdu.edu.cn/showproblem.php?pid=2203
题目意思很简单,求s1串所构成的环中是否有s2这个串
用CMP参考http://s.acmore.net/show_article/show/44
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
#include <set>
#include <queue>
#define MAX(a,b) (a) > (b)? (a):(b)
#define MIN(a,b) (a) < (b)? (a):(b)
#define mem(a) memset(a,0,sizeof(a))
#define INF 1000000007
#define MAXN 100005
using namespace std; char a[*MAXN],b[MAXN];
int next[MAXN]; void get_next()
{
next[]=-;
int key = -;
int index = ;
int len =strlen(b);
while(index < len-)
{
if(key == - || b[index] == b[key])
{
next[++index] = ++key;
}
else{
key = next[key];
}
}
} int KMP()
{
int A=,B=;
int len1 = strlen(a);
int len2 = strlen(b);
while(A<len1 && B<len2)
{
if(B==- || a[A] == b[B])
{
A++;B++;
}
else
{
B=next[B];
}
}
if(B >= len2)return ;
return ;
} int main()
{
while(~scanf("%s%s",a,b))
{
int len = strlen(a);
for(int i=; i<len; i++)
{
a[len+i] = a[i];
}
get_next();
printf("%s\n",KMP()?"yes":"no");
}
return ;
}