• <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>

            HDOJ 1841 Find the Shortest Common Superstring (最小公共父串)

            Problem Description
            The shortest common superstring of 2 strings S1 and S2 is a string S with the minimum number of characters which contains both S1 and S2 as a sequence of consecutive characters. For instance, the shortest common superstring of “alba” and “bacau” is “albacau”.
            Given two strings composed of lowercase English characters, find the length of their shortest common superstring.
             

            Input
            The first line of input contains an integer number T, representing the number of test cases to follow. Each test case consists of 2 lines. The first of these lines contains the string S1 and the second line contains the string S2. Both of these strings contain at least 1 and at most 1.000.000 characters.
             

            Output
            For each of the T test cases, in the order given in the input, print one line containing the length of the shortest common superstring.
             

            Sample Input
            2
            alba
            bacau
            resita
            mures
             

            Sample Output
            7
            8
               題目要求2個(gè)字符串的shortest common superstring,最小公共父串:使得str1和str2都包含在所求的superstring中,且該superstring的長度最小。設(shè)str1的長度為m,str2的長度為n,如果按照逐個(gè)位置匹配求公共部分的方法,時(shí)間復(fù)雜度為O(m*n)。題目中的n∈[1,1000000],顯然會(huì)TLE,必須降低時(shí)間復(fù)雜度,只能用KMP模式匹配算法來做了,時(shí)間復(fù)雜度O(m+n)。
               用KMP算法來求解時(shí),分3種情況:
               ----str1與str2能完全匹配,長度為max{len1,len2},如banana,ban;
               ----str1與str2不能匹配,但是能找到公共部分,如字符串a(chǎn)lba,bacau,公共部分ba長度為2;
               ----str1與str2不能匹配,且沒有公共部分,如asdf,ghjk。
             1 #include <iostream>
             2 
             3 #define max(a,b) (a>b?a:b)
             4 #define match(a,b) ((a)==(b))
             5 const int MAXN = 1000001;
             6 int fail[MAXN];
             7 char str[2][MAXN];
             8 
             9 int kmp_pat_match(char *str,int ls,int &i,char *pat,int lp,int &j){
            10     fail[0]=-1,i=0,j;
            11     for(j=1;j<lp;j++){
            12         for(i=fail[j-1];i>=0 && !match(pat[i+1],pat[j]);i=fail[i]);
            13         fail[j]=match(pat[i+1],pat[j])?i+1:-1;
            14     }
            15     for(i=j=0;i<ls&&j<lp;i++)
            16         if(match(str[i],pat[j])) j++;
            17         else if(j)
            18             j=fail[j-1]+1,i--;
            19     return j==lp?(i-lp):-1;
            20 }
            21 int main(){
            22     int i,j,t,u,v,len1,len2,pos;
            23     scanf("%d",&t),getchar();
            24     while(t--){
            25         gets(str[0]),gets(str[1]);
            26         len1=strlen(str[0]),len2=strlen(str[1]);
            27         u=0,pos=kmp_pat_match(str[0],len1,i,str[1],len2,j);
            28         if(pos!=-1) u=len2;
            29         else if(i==len1 && j>0 && match(str[0][len1-1],str[1][j-1])) u=j;
            30         v=0,pos=kmp_pat_match(str[1],len2,i,str[0],len1,j);
            31         if(pos!=-1) v=len1;
            32         else if(i==len2 && j>0 && match(str[1][len2-1],str[0][j-1])) v=j;
            33         printf("%d\n",len1+len2-max(u,v));
            34     }
            35     return 0;
            36 }

            posted on 2009-04-28 13:38 極限定律 閱讀(619) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年4月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            亚洲国产精品久久久久网站 | 性做久久久久久久久| 久久九九全国免费| 久久精品中文字幕有码| 亚洲美日韩Av中文字幕无码久久久妻妇 | 99久久国产亚洲综合精品| 国产成人无码精品久久久性色| 久久久久亚洲AV无码麻豆| 久久精品国产WWW456C0M| 青青草原综合久久大伊人| 国产一级持黄大片99久久| 日产久久强奸免费的看| 久久这里只有精品18| 超级97碰碰碰碰久久久久最新| 亚洲va久久久噜噜噜久久| 性做久久久久久久久| 国产成人综合久久综合| 国产色综合久久无码有码| 国产精品欧美亚洲韩国日本久久| 久久久久久亚洲Av无码精品专口| 久久99精品国产麻豆不卡| 久久国产成人午夜AV影院| 久久国产亚洲高清观看| 精品一二三区久久aaa片| 亚洲中文字幕无码一久久区| 久久亚洲精品无码播放| 久久人人爽人爽人人爽av| 品成人欧美大片久久国产欧美...| 欧美一区二区三区久久综| 亚洲精品乱码久久久久久蜜桃 | 欧美一区二区久久精品| 国内精品久久久久久久亚洲| 久久综合中文字幕| 精品九九久久国内精品| 丁香狠狠色婷婷久久综合| 久久99久久99精品免视看动漫| 久久精品人人槡人妻人人玩AV | 久久91精品国产91久久麻豆| 久久婷婷五月综合97色| 亚洲中文字幕无码久久综合网| 亚洲精品午夜国产VA久久成人 |