HDU 2203 亲和串 KMP

                                              

题意:如题

技巧 由于s1串是环形则将s1串扩大2倍 及 如123 串则 变为123123

裸KMP

ACcode:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
char A[maxn<<1],B[maxn];
int nextx[maxn];
int lenA,lenB;
void Get_nextx(){
    int j=0,k=-1;
    nextx[0]=-1;
    while(j<lenB){
        if(k == -1 || B[j] == B[k]){
            nextx[++j]=++k;
        }
        k=nextx[k];
        //printf("k==%d\n",k);
    }
}
int KMP(){
    Get_nextx();
    int i=0,j=0;
    while(i < lenA && j<lenB){
        if(j == -1 || A[i] == B[j]){
            i++;j++;
        }
        else j=nextx[j];
        //printf("%d %d\n",i,j);
    }
    if(j == lenB) return 1;
    else          return 0;
}
int main(){
    while(scanf("%s%s",A,B)!=EOF){
       // printf("%s\n%s\n",A,B);
        int len=strlen(A);
        for(int i=0,j=len;i<len;i++,j++){ //扩展成环形
            A[j]=A[i];
        }
        lenA=strlen(A);
        lenB=strlen(B);
        int res =KMP();
        if(res == 1) puts("yes");
        else         puts("no");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值