码迷,mamicode.com
首页 > 编程语言 > 详细

POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)

时间:2017-12-13 23:28:04      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:creat   asi   last   字典   call   you   other   cte   暴力   

Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.

After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.

Your task is to find such a sequence.

Input

The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.

After the last trademark, the next task begins. The last task is followed by a line containing zero.

Output

For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.

Sample Input

3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

Sample Output

abb
IDENTITY LOST

 

题意:

求n个串的最长公共字串,如果有多个,输出最小字典序的一个。

思路:

KMP||后缀数组||广义后缀自动机,不说了,上高数课了。代码比较暴力。下课了再试一试优化。

对比:

后缀数组SA已经排序了,最小字典序好找。而后缀自动机则需要像字典树一样搜索。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<memory>
#include<cmath>
using namespace std;
int n,len,ans,Max,now;
const int maxn=4000010; 
char s[2100],cap[2100];
struct SAM
{
    int ch[maxn][26],fa[maxn],maxlen[maxn],Last,sz;
    int root,nxt[maxn],size[maxn];bool Flag;
    void init()
    {
        sz=0;Flag=false;
        root=++sz;
        memset(size,0,sizeof(size));
        memset(ch[1],0,sizeof(ch[1]));
        memset(nxt,0,sizeof(nxt));
    }
    void add(int x)
    {
        int np=++sz,p=Last;Last=np;
        memset(ch[np],0,sizeof(ch[np]));
        maxlen[np]=maxlen[p]+1;
        while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
        if(!p) fa[np]=1;
        else {
            int q=ch[p][x];
            if(maxlen[p]+1==maxlen[q]) fa[np]=q;
            else {
                int nq=++sz;
                memcpy(ch[nq],ch[q],sizeof(ch[q]));size[nq]=size[q]; nxt[nq]=nxt[q];
                maxlen[nq]=maxlen[p]+1;
                fa[nq]=fa[q];
                fa[q]=fa[np]=nq;
                while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
            }
        }
        for(;np;np=fa[np]) 
          if(nxt[np]!=now) {
              size[np]++;
              nxt[np]=now;
          }else break;
    }
   /* void dfs(int x,int d){//输出    
       if(Flag||d>n) return;
       if(d==n){ puts(cap); Flag=true; return; }
          for(int i=0;i<26;i++)
          if(ch[x][i]&&size[ch[x][i]]==n){ cap[d]=i+‘a‘; dfs(ch[x][i],d+1); cap[d]=0; }
    }*/
    void dfs(int x,int d){//输出    
       if(d!=maxlen[x]||d>ans||Flag) return;
        if(maxlen[x]==ans&&size[x]>=n) { puts(cap); Flag=true; return; }
          for(int i=0;i<26;++i)
          if(ch[x][i]){ cap[d]=i+a; dfs(ch[x][i],d+1); cap[d]=0; }
    }
};
SAM Sam;
int main()
{
    while(~scanf("%d",&n)&&n){     
        Sam.init();
        for(int i=1;i<=n;i++) {
            scanf("%s",s+1);
            Sam.Last=Sam.root;
            len=strlen(s+1);
            now=i;
            for(int j=1;j<=len;j++) Sam.add(s[j]-a);
        }
        ans=0;
        for(int i=1;i<=Sam.sz;i++) 
            if(Sam.size[i]==n&&Sam.maxlen[i]>ans) ans=Sam.maxlen[i];
        if(ans)  Sam.dfs(1,0);
        else printf("IDENTITY LOST\n");
    }
    return 0;
}

 

POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)

标签:creat   asi   last   字典   call   you   other   cte   暴力   

原文地址:http://www.cnblogs.com/hua-dong/p/8034241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!