/*
题目描述:给出两个字符串s1 、 s2 , 问s1经过循环之后(如AABCD经过循环可以变成ABCDA,BCDAA,CDAAB,DAABC),
能否使s2包含在s1之中?
思路:如果s2的长度大于s1的长度,直接输出no,否则将两个s1拼起来,比如AABCD拼成AABCDAABCD,那么循环能
形成的全部字符串都包含在了拼接后的字符串当中,直接在拼接后的字符串中找s2是否出现,使用kmp即可
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxn = 3e5 + 5;
const int maxs = 1e5 + 5;
char s1[maxn] , s2[maxs] , temp[maxn];
int f[maxs] , flag ;
void getFail( char * P , int * f)
{
int m = strlen(P);
f[0] = f[1] = 0;
for(int i = 1 ; i<m ; i++){
int j = f[i];
while(j && P[i] != P[j])
j = f[j];
if(P[i] == P[j]) f[i + 1] = j + 1;
else f[i + 1] = 0;
}
}
void KMP( char * T , char * P , int * f)
{
int m = strlen(P) , n = strlen(T) ;
getFail( P , f );
int j = 0;
for(int i = 0 ; i<n ; i++){
while(j && T[i] != P[j])
j = f[j];
if( T[i] == P[j] ) ++j;
if(j == m){
flag = 1 ;
}
}
}
int main()
{
while(scanf("%s%s",temp,s2)!=EOF){
int len1 = strlen(temp);
int len2 = strlen(s2) ;
if(len1 < len2){
printf("no\n") ;
continue;
}
for(int i = 0 ; i<len1 ; i++){
s1[i] = s1[i + len1] = temp[i] ;
}
s1[2 * len1] = 0;
flag = 0;
mem(f , 0);
KMP(s1 , s2 , f);
if(flag)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
hdu2203 亲和串
最新推荐文章于 2021-04-25 21:49:41 发布