标签:kmp
AABCD CDAA ASD ASDF
yes no
/*************************************************************************
> File Name: hdu2203.cpp
> Author: ALex
> Mail: 405045132@qq.com
> Created Time: 2015年01月05日 星期一 11时06分38秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int next[N];
char str[N];
char text[N << 1];
void get_next()
{
int len = strlen(str);
int j = 0;
int k = -1;
next[0] = -1;
while (j < len)
{
if (k == -1 || str[j] == str[k])
{
next[++j] = ++k;
}
else
{
k = next[k];
}
}
}
bool KMP()
{
int len1 = strlen(text);
int len2 = strlen(str);
int i = 0;
int j = 0;
while (j < len2 && i < len1)
{
if (j == -1 || str[j] == text[i])
{
++i;
++j;
}
else
{
j = next[j];
}
}
if (j == len2)
{
return 1;
}
return 0;
}
int main()
{
while (~scanf("%s%s", text, str))
{
get_next();
int len = strlen(text);
for (int i = len; i < 2 * len - 1; ++i)
{
text[i] = text[i - len];
}
text[2 * len - 1] = '\0';
if (KMP())
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}标签:kmp
原文地址:http://blog.csdn.net/guard_mine/article/details/42419867