hdu kmp 1711 Number Sequence

來源:互聯網
上載者:User

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

在這道題目中  我糾結了很久  一直不能提交 測試能成功    為什麼交不了呢   我看了一下別人代碼   發現差不多    但是最後還是發現有問題  

在kmp()函數中if(j==0||s2[i]==s1[j])  我把 j 寫成了0了  其實 j=-1
的。所以如果交不了  可然還是自己的錯了   一個細節就不能成功   細節也是很重要的     

多向自己方面想想那錯了   那你一定可以正確的  呵呵  

  ac 代碼

#include<iostream>#include<cstring>#include<cstdio>#include<iomanip>using namespace std;int s1[10003],s2[1000003];int next[10003];int N,M;void get_next(){    int i=0;    int j=-1;    next[0]=-1;    while(i<M)    {        if(j==-1||s1[i]==s1[j])        {            i++,j++;            next[i]=j;        }        else j=next[j];    }}int kmp(){    int i=0;    int j=0;    next[1]=0;    if(M>N) return -1;    while(i<N&&j<M)    {        if(j==-1||s2[i]==s1[j])        {            i++,j++;        }        else  j=next[j];    }    if(j>=M) return i-M+1;    else return -1;}int main(){    int t,k;         cin>>t;        while(t--)        {            cin>>N>>M;            for(int i=0;i<N;i++) cin>>s2[i];            for(int i=0;i<M;i++) cin>>s1[i];            get_next();            k= kmp();            cout<<k<<endl;        }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.