• <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
            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(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ù),  然后自己寫(xiě)個(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;
            }
            亚洲欧美日韩久久精品第一区| 国产精品美女久久久| 中文字幕久久亚洲一区| 2021国产精品午夜久久| 亚洲AV乱码久久精品蜜桃| 91精品日韩人妻无码久久不卡| 亚洲а∨天堂久久精品| 成人国内精品久久久久一区| 人妻精品久久久久中文字幕 | 久久精品国产亚洲av高清漫画| 99久久精品国产高清一区二区 | 国产精品久久久久久久久免费| 久久国产V一级毛多内射| 亚洲中文久久精品无码| 久久97久久97精品免视看| 久久婷婷成人综合色综合| 久久免费观看视频| 久久亚洲国产精品一区二区| 久久久久亚洲AV片无码下载蜜桃 | 久久精品国产精品亚洲| 精品久久人妻av中文字幕| 成人综合久久精品色婷婷| 国产精品九九久久免费视频| 久久久久久午夜成人影院| 国产成人精品久久| 人妻中文久久久久| 亚洲伊人久久综合影院| 久久久久九九精品影院| 国内精品久久久久久久久电影网 | 久久人做人爽一区二区三区 | 国内精品久久国产| 综合久久给合久久狠狠狠97色| 国内精品久久久久影院网站| 国产A级毛片久久久精品毛片| 国产一区二区三区久久精品| 国产精品9999久久久久| 99久久免费国产特黄| 国产午夜精品理论片久久影视 | 国产精品国色综合久久| 粉嫩小泬无遮挡久久久久久| 久久夜色精品国产噜噜亚洲AV|