• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ACM___________________________

            ______________白白の屋
            posts - 182, comments - 102, trackbacks - 0, articles - 0
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            MiYu原創(chuàng), 轉(zhuǎn)帖請(qǐng)注明 : 轉(zhuǎn)載自 ______________白白の屋    

             

             

             

            題目地址 :

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

            題目描述:

            代碼
            Number Sequence

            Time Limit: 
            10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
            Total Submission(s): 
            1926    Accepted Submission(s): 819


            Problem Description
            Given two sequences of numbers : a[
            1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 100001 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1= b[2], ...... , a[K + M - 1= b[M]. If there are more than one K exist, output the smallest one.
             

            Input
            The first line of input 
            is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 100001 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-10000001000000].
             

            Output
            For each test 
            case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
             

            Sample Input
            2
            13 5
            1 2 1 2 3 1 2 3 1 3 2 1 2
            1 2 3 1 3
            13 5
            1 2 1 2 3 1 2 3 1 3 2 1 2
            1 2 3 2 1
             

            Sample Output
            6
            -1
             

             

             代碼

            題目分析 :
               很久以前做的一個(gè)題目,  純粹的套模板了..... 到現(xiàn)在還是沒(méi)明白 KMP 到底怎么回事, 也可能是我沒(méi)花時(shí)間去仔細(xì)的看的原因.

            今天上郵箱發(fā)現(xiàn)一個(gè)星期前一位網(wǎng)友問(wèn)我的1711代碼速度為什么那么快, 著到代碼一看, 原來(lái)是個(gè) 簡(jiǎn)單的KMP 裸題, 直接模板就行了,

            速度的話這里有個(gè)小技巧, 知道的人就不要笑話我了.  

            其實(shí)關(guān)鍵就是 對(duì)輸入數(shù)據(jù)的 加速
            !!!!
            一般對(duì)數(shù)據(jù)的輸入都是 
            %d 或 %%lf 對(duì)吧?  其實(shí)計(jì)算機(jī)讀取的數(shù)據(jù)是字符形的, 它也需要調(diào)用其他的函數(shù)將這個(gè)串轉(zhuǎn)換成
            數(shù)字,  所以你只要 用字符串讀入 數(shù)據(jù),  然后自己寫個(gè)函數(shù) 把 這個(gè)字符串轉(zhuǎn)換成 數(shù)字久行了.

            其實(shí)呢, 很多題目都可以只要來(lái)加速的, 除非它的數(shù)據(jù)量不大.


            下面是我的代碼 : 


            #include 
            <iostream>
            using namespace std;
            const int MAXN=1000000;
            const int MAXM=10000;

            int nn[MAXN+1],mm[MAXM+1];
            int Fail[MAXM+1];

            void GetFail(int num[],int m){
                    Fail[
            0]=-1;
                    
            for(int i=1,j=-1;i<m;i++){
                            
            while(j>=0&&num[j+1]!=num[i]){
                                    j
            =Fail[j];
                            }
                            
            if(num[j+1]==num[i])j ++;
                            Fail[i]
            =j;
                    }
            }
            int KMP(int numA[],int numB[],int n,int m){
                    GetFail ( numB,m );    
                    
            for (int i=0,j=0;i<n;i++){
                            
            while(j&&numB[j]!=numA[i]){
                                    j
            =Fail[j-1]+1;
                            }
                            
            if(numB[j]==numA[i]) j++;
                            
            if(j == m) return i-m+2;
                    }
                    
            return -1;
            }
            inline 
            bool scan_d(int &num)  //  這個(gè)就是 加速的 關(guān)鍵了 
            {
                    
            char in;bool IsN=false;
                    
            in=getchar();
                    
            if(in==EOF) return false;
                    
            while(in!='-'&&(in<'0'||in>'9')) in=getchar();
                    
            if(in=='-'){ IsN=true;num=0;}
                    
            else num=in-'0';
                    
            while(in=getchar(),in>='0'&&in<='9'){
                            num
            *=10,num+=in-'0';
                    }
                    
            if(IsN) num=-num;
                    
            return true;
            }
            int main ()
            {
                
            int N,M,T;
                
            while ( scan_d(T) )
                {
                       
            while ( T -- )
                       {
                              scan_d(N),scan_d(M);
                              
            for ( int i = 0; i != N; ++ i )
                                  scan_d ( nn[i] );
                              
            for ( int j = 0; j != M; ++ j )
                                  scan_d ( mm[j] );    
                              printf ( 
            "%d\n",KMP ( nn,mm,N,M ) );  
                       }
                }
                
            return 0;
            }
            色老头网站久久网| AA级片免费看视频久久| 性欧美大战久久久久久久久| 久久棈精品久久久久久噜噜| 国产高清美女一级a毛片久久w| 91久久精品视频| 久久国产精品77777| 久久精品国产久精国产| 精产国品久久一二三产区区别| 尹人香蕉久久99天天拍| 久久青青草原综合伊人| 午夜人妻久久久久久久久| 国产精品九九九久久九九| 久久久久99精品成人片直播| 久久99精品国产麻豆不卡| 久久精品人人做人人爽电影| 亚洲人成网站999久久久综合| 亚洲精品乱码久久久久久久久久久久 | 久久九九兔免费精品6| 国产精品久久网| 伊人久久大香线蕉综合热线| 国产成人久久精品一区二区三区| 狠狠综合久久AV一区二区三区| 国内精品久久久久| 狠狠色丁香久久婷婷综合| 国产91久久综合| 久久男人Av资源网站无码软件| 久久久久婷婷| 久久亚洲精品无码观看不卡| 久久伊人影视| 四虎国产精品免费久久久| 亚洲综合日韩久久成人AV| 久久人妻少妇嫩草AV蜜桃| 99久久精品费精品国产一区二区 | 亚洲人AV永久一区二区三区久久| 好属妞这里只有精品久久| 99久久99久久精品国产片果冻| 人妻系列无码专区久久五月天| 久久只有这精品99| 久久久久这里只有精品| 青青草原综合久久|