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

ecnu 3441 Kmp

时间:2018-06-20 10:20:00      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:pac   AC   out   its   col   names   pre   int   nbsp   

#include<bits/stdc++.h>
using namespace std;

int kmp(string& t,string& p){   //从t中找p出现的次树
    int* next=new int[p.size()+5];
    next[0]=next[1]=0;
    int cnt=0,lent=t.size(),lenp=p.size();
    for(int i=1;i<lenp;i++){
        int j=next[i];
        while(j>0&&p[i]!=p[j])j=next[j];
        next[i+1]=p[i]==p[j]?j+1:0;
    }
    for(int i=0,j=0;i<lent;i++){
        while(j>0&&t[i]!=p[j])j=next[j];
        if(t[i]==p[j])j++;
        if(j==lenp){
            j=next[j];
            cnt++;
        }
    }
    return cnt;
}

int main(){
    string t,p;
    cin>>t;
    int n,u,l,r;
    cin>>n;
    while(n--){
        cin>>l>>r>>p;
        string sub(t.substr(l,r-l+1));
        int res=kmp(sub,p);
        cout<<res<<endl;
    }
}

 

ecnu 3441 Kmp

标签:pac   AC   out   its   col   names   pre   int   nbsp   

原文地址:https://www.cnblogs.com/TAMING/p/9202205.html

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