poj 3450 Corporate Identity(后缀数组 多个字符串的最长公共字串)

来源:互联网 发布:网络信息有限公司 编辑:程序博客网 时间:2024/06/07 01:08

Corporate Identity
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 7303 Accepted: 2557

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

3aabbaabbabbababbbbbbbabb2xyzabc0

Sample Output

abbIDENTITY LOST


解:类似于二个字符串的操作,用id数组记录每一个下标属于哪个字符串,当二分枚举答案时如当前字符串恰好出现在所有字符串中出现过则返回真

清空数组开成bool 快了将近1s



#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;const int N = 1000000;int sa[N];int t1[N],t2[N],c[N];int rank1[N],height[N];void build_sa(int s[],int n ,int m){    int i,j,p,*x=t1,*y=t2;    for(int i=0;i<m;i++) c[i]=0;    for(int i=0;i<n;i++) c[x[i]=s[i]]++;    for(int i=0;i<m;i++) c[i]+=c[i-1];    for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;    for(j=1;j<=n;j<<=1)    {        p=0;        for(i=n-j;i<n;i++) y[p++]=i;        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;        for(i=0;i<m;i++) c[i]=0;        for(i=0;i<n;i++) c[x[y[i]]]++;        for(i=0;i<m;i++) c[i]+=c[i-1];        for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;x[sa[0]]=0;        for(i=1;i<n;i++)            x[sa[i]]=(y[sa[i-1]]==y[sa[i]])&&(y[sa[i-1]+j]==y[sa[i]+j])?p-1:p++;        if(p>=n) break;        m=p;    }}int s[N];void getheight(int s[],int n){    int i, j, k=0;    for(i=1;i<=n;i++) rank1[sa[i]]=i;(1->n)    for(i=0;i<n;i++)    {        if(k) k--;        j=sa[rank1[i]-1];        while(s[i+k]==s[j+k]) k++;        height[rank1[i]]=k;    }    return ;}char str1[N], str2[N];int id[N],  sum;bool vis[4005];int judge(int mid,int n,int k){    memset(vis,0,sizeof(vis));    int i, j,  cnt=0;    for(int i=2;i<=n;i++)    {        if(height[i]<mid)        {            memset(vis,0,sizeof(vis));            cnt=0;        }        else        {            if(!vis[id[sa[i-1]]])            {                cnt++;                vis[id[sa[i-1]]]=1;            }            if(!vis[id[sa[i]]])            {                cnt++;                vis[id[sa[i]]]=1;            }            if(cnt==k)            {                sum=sa[i];                return 1;            }        }    }    return 0;}int main(){    int n, i, j, k, len;    while(scanf("%d",&k),k!=0)    {        n=0;        for(int i=0;i<k;i++)        {            scanf("%s",str1);            len=strlen(str1);            for(j=0;j<len;j++)            {                s[n]=str1[j];id[n]=i;                n++;            }            s[n]='#'+i;id[n]='#'+i;            n++;        }        s[n]=0;        build_sa(s,n+1 ,5000);        getheight(s,n);        int l=1, r=len, mid, flag=0;        while(l<=r)        {            mid=(l+r)/2;            if(judge(mid,n,k)) flag=mid,l=mid+1;            else r=mid-1;        }        if(flag)        {            for(j = 0; j<flag; j++) str2[j] = s[sum+j];            str2[flag] = '\0';            printf("%s\n",str2);        }        else printf("IDENTITY LOST\n");    }    return 0;}

阅读全文
0 0