码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1686 Oulipo

时间:2017-03-31 18:08:12      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:.com   简单   std   ret   class   blog   pac   amp   image   

传送门;http://acm.hdu.edu.cn/showproblem.php?pid=1686

技术分享

技术分享

解题思路:简单的kmp算法。

 实现代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN=1000010;
const int MAXM=10010;

void makeNext(const char P[],int next[]){
    int m=strlen(P);
    int k;
    memset(next,0,sizeof(next));
    for(int i=1,k=0;i<m;i++){
        if(k>0&&P[k]!=P[i])
            k=next[k-1];

        if(P[k]==P[i])
            k++;
        next[i]=k;
    }
}

int kmp(const char T[],const char P[],int next[]){
    int ans=0;
    makeNext(P,next);
    int m=strlen(P);
    int n=strlen(T);
    for(int i=0,k=0;i<n;i++){
        if(k>0&&P[k]!=T[i])
            k=next[k-1];

        if(P[k]==T[i])
            k++;
        if(k==m){
            ans++;
        }
    }
    return ans;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        char T[MAXN];
        char P[MAXM];
        int next[MAXM];
        scanf("%s",P);
        scanf("%s",T);
        int ans=kmp(T,P,next);
        printf("%d\n",ans);
    }
}

 

HDU 1686 Oulipo

标签:.com   简单   std   ret   class   blog   pac   amp   image   

原文地址:http://www.cnblogs.com/IKnowYou0/p/6652609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!