• <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>
            posts - 43,  comments - 9,  trackbacks - 0
            后綴數組,KMP擴展,自動機
            題目要求4000個長度為200的字串的最長公共子串.
            只需選一個串作為基串, 枚舉它的所有后綴串S[i..len], 求出其它串與這個子串的LCP. 最后選最大的輸出.
            關于求串S與串T所有后綴的LCP, 林希德的論文講了擴展KMP算法. 不過我想出另一種方法:
            把S和T直接連接得到新串U. 按正常KMP的方法求U的next數組, 不過當p指向U[len[S]+1]即原T串的首字母時, 直接將此位的next置為0.
            最后min(len[S], next[k]) 就是串T[1..(k-len[S])]與串S的lcp長度.

            代碼如下:

             1#include <iostream>
             2using namespace std;
             3
             4char wd[4000][201];
             5int cnt[201][201], vis[201][201];
             6char ss[401];
             7int next[401];
             8int N;
             9
            10bool readin()
            11{
            12  scanf("%d",&N);
            13  if(N==0)return false;
            14  
            15  for(int i=0; i<N; i++)
            16    scanf("%s",wd[i]);
            17  return true;
            18}

            19
            20int mycmp(char *s1, int l1, char *s2, int l2)
            21{
            22  if(l1!=l2) return (l2-l1);
            23  while(--l1 && *s1==*s2) s1++, s2++;
            24  return (*s1-*s2);
            25}

            26
            27void mycpy(char *s1, char *s2, int l)
            28{
            29  while(l--*(s1++)=*(s2++);
            30  *s1=0;
            31}

            32
            33void solve()
            34{
            35  memset(cnt,0,sizeof(cnt));
            36  memset(vis,0,sizeof(vis));
            37  
            38  int l0=strlen(wd[0]);
            39  strcpy(ss,wd[0]);
            40  char ans[201]="";
            41  
            42  for(int p=0; p<l0; p++){
            43    char *s=&ss[p];
            44    for(int i=1; i<N; i++){
            45      strcpy(&ss[l0], wd[i]); //連接兩個串
            46      int l1=l0-p+strlen(wd[i]);
            47      next[0]=-1;
            48      int p0=-1, p1=0;
            49      while(p1<l1){
            50        while(p0>=0 && s[p0]!=s[p1])
            51          p0=next[p0];
            52        next[++p1]=++p0;
            53        if(p1==l0-p) //此位置0
            54          next[p1]=0, p0=0;
            55        if(p1>=l0-p){
            56          int len=min(l0-p, next[p1]);
            57          if(vis[p][p+len-1]!=i)
            58            vis[p][p+len-1]=i, cnt[p][p+len-1]++;
            59        }

            60      }

            61    }

            62    
            63    for(int j=l0-1; j>=p; j--){
            64      if(cnt[p][j]==N-1){
            65        if(mycmp(&ss[p], j-p+1, ans, strlen(ans))<0)
            66          mycpy(ans, &ss[p], j-p+1);
            67      }

            68    }

            69  }

            70  
            71  if(ans[0]==0)
            72    puts("IDENTITY LOST");
            73  else
            74    puts(ans);
            75}

            76
            77int main()
            78{
            79  while(readin()){
            80    solve();
            81  }

            82}

            83


            posted on 2009-07-31 13:46 wolf5x 閱讀(703) 評論(0)  編輯 收藏 引用 所屬分類: acm_icpc
            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            "Do not spend all your time on training or studying - this way you will probably become very exhausted and unwilling to compete more. Whatever you do - have fun. Once you find programming is no fun anymore – drop it. Play soccer, find a girlfriend, study something not related to programming, just live a life - programming contests are only programming contests, and nothing more. Don't let them become your life - for your life is much more interesting and colorful." -- Petr

            留言簿(3)

            隨筆分類(59)

            隨筆檔案(43)

            cows

            搜索

            •  

            最新評論

            評論排行榜

            久久久久久久99精品免费观看| 欧美日韩久久中文字幕| 精品久久久久久国产| 中文精品久久久久人妻| 伊人久久大香线蕉成人| 99精品国产综合久久久久五月天| 午夜欧美精品久久久久久久| 国产综合久久久久久鬼色| 国产精品成人99久久久久| 99久久免费国产精品特黄| 久久大香香蕉国产| 国产成人精品久久综合| 亚洲国产精品无码久久青草| 色综合久久久久久久久五月| 欧美久久精品一级c片片| 精品久久久久成人码免费动漫| 久久一日本道色综合久久| 国产香蕉97碰碰久久人人| 久久伊人五月丁香狠狠色| 99国内精品久久久久久久| 久久天天躁狠狠躁夜夜躁2014| 久久精品免费观看| 亚洲综合精品香蕉久久网| 国产精品免费看久久久香蕉| 亚洲人成精品久久久久| 久久人妻少妇嫩草AV无码蜜桃 | 99久久国语露脸精品国产| 久久久久无码中| 久久国产乱子精品免费女| 丁香色欲久久久久久综合网| 久久影院午夜理论片无码| 伊人久久大香线蕉影院95| 国产午夜精品久久久久免费视| 久久综合亚洲色一区二区三区| 久久天天躁狠狠躁夜夜2020| 7国产欧美日韩综合天堂中文久久久久| 中文字幕热久久久久久久| 日本欧美国产精品第一页久久| 国产免费久久久久久无码| 久久这里只有精品首页| 国产精品久久久久天天影视|