# 亲和串

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10185    Accepted Submission(s): 4661

Problem Description

Input

Output

Sample Input
AABCD CDAA ASD ASDF

Sample Output
yes no

Author
Eddy

Recommend
lcy   |   We have carefully selected several similar problems for you: 3336 2201 2202 2200 2206

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   ind<<1,le,mid
#define rson    ind<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=100000 +20   ;
//const int maxm=    ;

char N[2*maxn];
char M[maxn];
int nex[maxn];
int m;
void getnex()
{
nex[0]=-1;
for(int i=0;M[i];i++)
{
int j=nex[i];
while(~j&&M[i]!=M[j]) j=nex[j];
nex[i+1]=M[i+1]==M[j+1]?nex[j+1]:j+1;
}

}

bool KMP()
{
m=strlen(M);
int j=0;
for(int i=0;N[i];i++)
{
while(~j&&N[i]!=M[j])  j=nex[j];
if(++j==m) return true;
}
return false;
}

int main()
{
while(~scanf("%s",N))
{
strcpy(M,N);
strcat(N,M);
scanf("%s",M);
getnex();
if(KMP())  puts("yes");
else puts("no");
}

return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：hdu 2203 亲和串 KMP 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)