欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj3450

程序员文章站 2022-05-11 23:20:27
...

题目

Corporate Identity
Time Limit: 3000MS
Memory Limit: 65536K

Description

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

题意

给多个字符串,求他们的最长公共子串

题解

考虑一下一组最优解所对应开头的后缀在sa中一定是连续的
所以我们可以先二分一下答案,然后用一个标记数组来记录现在的解在哪些字符串里面出现过,如果某一时刻height[i]< k,那么就表示这个答案是不合法的,清空后再重新做就好了

贴代码

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;

const int maxn=205,ma1=800005,mage=4005;
int sa[ma1],rank[ma1],x[ma1],y[ma1],buc[ma1],wei[ma1],height[ma1];
int le[mage],chang[mage],pp[mage];
char s1[maxn],s[ma1];
bool bz[mage];
int i,j,k,l,r,n,m,len,p,mid,t1,t2,now,fi;

void gesa(){
    m=96+26;
    fo(i,0,len-1) x[i]=s[i];
    fo(i,0,m) buc[i]=0;
    fo(i,0,len-1) buc[x[i]]++;
    fo(i,1,m) buc[i]+=buc[i-1];
    fo(i,0,len-1) sa[buc[x[i]]--]=i;
    for(k=1;k<len;k=k*2){
        p=0;
        fo(i,len-k,len-1) y[++p]=i;
        fo(i,1,len) if (sa[i]>=k) y[++p]=sa[i]-k;
        fo(i,1,m) buc[i]=0;
        fo(i,0,len-1) buc[x[i]]++;
        fo(i,1,m) buc[i]+=buc[i-1];
        for(i=len;i>=1;i--) sa[buc[x[y[i]]]--]=y[i];
        fo(i,0,len-1) y[i]=x[i];
        x[sa[1]]=1;
        fo(i,2,len){
            if (y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k]) x[sa[i]]=x[sa[i-1]]; else
            x[sa[i]]=x[sa[i-1]]+1;
        }
        m=x[sa[len]];
        if (m>=len) break;
    }
    fo(i,1,len) rank[sa[i]]=i; 
}

void gehei(){
    k=0;
    fo(i,0,len-1){
        if (rank[i]==1){
            height[rank[i]]=0;
            continue;
        }
        if (k) k--;
        t1=i+k; t2=sa[rank[i]-1]+k;
        while (t1<len && t2<len){
            if (s[t1]=='#' || s[t2]=='#') break;
            if (s[t1]==s[t2]) k++; else break;
            t1++; t2++;
        }
        height[rank[i]]=k;
    }
}
int check(int v){
    memset(bz,false,(sizeof(bz)));
    now=0;
    fo(i,2,len)
    if (s[sa[i]]!='#')
    {
        if (i==20){
            i=i;
        }
        if (height[i]<v){
            fo(j,1,now){
                bz[pp[j]]=false;
            }
            now=0;
        } else{
            if (now==0){
                pp[++now]=wei[sa[i-1]];
                bz[pp[now]]=true;
            }
            if (! bz[wei[sa[i]]]){
                pp[++now]=wei[sa[i]];
                bz[pp[now]]=true;
                fi=sa[i];
            }
            if (now==n) return true;
        }
    }
    return false;
}
int main(){
    freopen("3450.in","r",stdin);
    freopen("3450.out","w",stdout);
    n=1;
    while (n){
        scanf("%d",&n);
        r=201;
        if (!n) break;
        fo(i,1,n){
            scanf("%s",&s1);
            p=strlen(s1);
            r=min(r,p);
            le[i]=le[i-1]+p;
            fo(j,le[i-1],le[i]-1) {
                s[j]=s1[j-le[i-1]];
                wei[j]=i;
            }
            s[le[i]++]='#';
        }
        len=le[n];
        gesa();
        gehei();
        l=0;
        while (l<r){
            mid=(l+r)/2;
            if (check(mid)) l=mid+1; else r=mid;
        }
        if (! check(l)){
            l--;
            bz[1]=check(l);
        }
        if (!l) printf("IDENTITY LOST\n"); else 
        {
            fo(i,fi,fi+l-1) printf("%c",s[i]);
            printf("\n");
        }
    }
    return 0;
}
相关标签: sa

推荐阅读