hdu 1686 kmp Oulipo

來源:互聯網
上載者:User

http://acm.hdu.edu.cn/showproblem.php?pid=1686

對於我這種剛開始學的人來說   剛開始我覺的很難  不會做哦    但是看了一下別人的一下子就知道了   還是不是太熟了    需要加強練習哦   呵呵

   其實這是一道簡單的kmp  的演算法     只要你在index_kmp()    將 i
 的值一直小於N  就可以了    還是看看我具體代碼吧     或許要看就知道了

#include<iostream>#include<cstring>#include<cstdio>using namespace std;char w[10005],t[1000005];int next[10005];int N,M;void get_next(){    int i=0,j=next[0]=-1;    while(i<M)    {        if(j==-1||w[i]==w[j])        {            i++,j++;            next[i]=j;        }        else j=next[j];    }}int kmp(){    int i=0,j=0;    int num=0;    while(i<N)    {        if(j==-1||t[i]==w[j])        {            i++,j++;        }        else j=next[j];        if(j==M)        {            num++;            j=next[j];        }    }  cout<<num<<endl;}int main(){    int c,h,i;    while(cin>>c)    {        while(c--)        {            scanf("%s",w);        scanf("%s",t);        N=strlen(t);        M=strlen(w);        get_next();        kmp();        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.