HDU 1711 Number Sequence(演算法驗證)

來源:互聯網
上載者:User

標籤:

該怎麼做、每一個人的人生都應該自己掌握、你給不了別人一切、你也不懂別人的憂傷、

                                                                                          微笑不代錶快樂、哭泣不一定悲傷

               不努力怎麼讓關心你的人幸福、不努力怎麼讓看不起你的人絕望、

                                                                                                                                    

                                                                                                                                                              我用生命在奮鬥——lx_Zz

—————————————————————————————————————————————————————————————

—————————————————————————    華麗的切割線    ————————————————————————————

—————————————————————————————————————————————————————————————

10678686 2014-05-05 03:11:35 Accepted 1711 468MS 4224K 1865 B C++

經過驗證、發現我剛剛YY的演算法目測是正確的、、O(∩_∩)O哈哈~效果還不錯。。

。→_→

淡淡改了一下、畢竟都是類似的題、廢話不多說、直接上代碼:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int str1[10005];int str2[1000005];int next[10005];int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)            scanf("%d",&str2[i]);        for(int i=0;i<m;i++)            scanf("%d",&str1[i]);        int len1=m;        next[0]=-1;        for(int i=1;i<len1;i++)        {            if(str1[i]==str1[next[i-1]])            {                next[i]=next[i-1]+1;            }            else if(str1[i]==str1[0])            {                next[i]=1;            }            else next[i]=0;        }        int len2=n;        int ans=-1;        int flag=0;        for(int i=0,j=0;i<len2;)        {            int ff=0;            if(str1[j]==str2[i])            {                i++;j++;                if(j>=len1)                {                    ans=i-m+1;                    break;                }            }            else            {                while(str1[j]!=str2[i])                {                    if(j==0)                    {                        ff=1;                        break;                    }                    j=next[j-1];                    if(j==-1)                    {                        ff=1;                        break;                    }                }                if(ff)                {                    if(str1[0]==str2[i])                    {                        i++;j=1;                    }                    else                    {                        i++;j=0;                    }                }            }        }        printf("%d\n",ans);    }    return 0;}


著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

HDU 1711 Number Sequence(演算法驗證)

聯繫我們

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