admin管理员组

文章数量:1616033

POJ 3450 Corporate Identity

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

1.暴力+KMP,找第一个字符串的所有子串与其他字符串匹配,中间过程比较迷,要尽可能减少计算量,否则会超时,AC代码如下:

#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
char s[4005][205],p[205];
int n[205];
void get(char p[]){
    int j=-1,i=0;
    n[0]=-1;
    int len1=strlen(p);
    while(i<len1){
        if(j==-1 || p[i]==p[j]){
            i++,j++;
            if(p[i]!=p[j]) n[i]=j;
            else n[i]=n[j];
        }
        else j=n[j];
    }
}

int kmp(char s[],char p[]){
    int i=0,j=0;
    int len1=strlen(s),len2=strlen(p);
    while(i<len1 && j<len2){
        if(j==-1 || s[i]==p[j]){
            i++,j++;
        }
        else j=n[j];
    }
    if(j==len2) return 1;
    return 0;
}

int main(){
    int m;
    while(~scanf("%d",&m) && m){
        char ans[205]="";
        for(int i=0;i<m;i++)
                scanf("%s",s[i]);
        int len=strlen(s[0]);
        for(int i=0;i<len;i++){
            for(int j=i;j<len;j++){
                int cnt=0;
                for(int k=i;k<=j;k++)
                    p[cnt++]=s[0][k];
                p[cnt]='\0';
                get(p);
                int flag=1;
                for(int k=0;k<m;k++){
                    if(!kmp(s[k],p)) {flag=0;break;}
                }
                if(flag){
                    if(strlen(p)==strlen(ans) && strcmp(p,ans)<0){
                        strcpy(ans,p);
                    }
                    else if(strlen(p)>strlen(ans))
                        strcpy(ans,p);
                }
            }
        }
        if(strcmp(ans,"")==0) puts("IDENTITY LOST");
        else printf("%s\n",ans);
    }
    return 0;
}

2.后缀数组,将所有字符串拼接起来,二分最大公共前缀,每一次二分都枚举所有 h e i g h t height height 数组,如果每个数组都大于等于二分的 m i d mid mid 值,且该前缀属于 n n n 个不同的字符串就符合条件,记录一下字符串的起点最后输出即可,AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long  ll;
const int maxn = 8e5+5;
int t,wa[maxn],wb[maxn],wsf[maxn],wv[maxn],sa[maxn];
int rk[maxn],height[maxn],s[maxn],vis[maxn],belong[maxn],anspos;
char str1[maxn],str2[maxn],p[maxn];

int cmp(int *r,int a,int b,int k)
{
    return r[a]==r[b]&&r[a+k]==r[b+k];
}

void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++)  wsf[i]=0;
    for(i=0; i<=n; i++)  wsf[x[i]=r[i]]++;
    for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
    for(i=n; i>=0; i--)  sa[--wsf[x[i]]]=i;
    p=1;
    j=1;
    for(; p<=n; j*=2,m=p)
    {
        for(p=0,i=n+1-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<=n; i++)  wv[i]=x[y[i]];
        for(i=0; i<m; i++)  wsf[i]=0;
        for(i=0; i<=n; i++)  wsf[wv[i]]++;
        for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
        for(i=n; i>=0; i--)  sa[--wsf[wv[i]]]=y[i];
        t=x;
        x=y;
        y=t;
        x[sa[0]]=0;
        for(p=1,i=1; i<=n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
    }
}

void getheight(int *r,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++)  rk[sa[i]]=i;
    for(i=0; i<n; i++)
    {
        if(k)
            k--;
        else
            k=0;
        j=sa[rk[i]-1];
        while(r[i+k]==r[j+k])
            k++;
        height[rk[i]]=k;
    }
}

int check(int x,int n)
{
    for(int i=1;i<=n-1;i++)
    {
        if(height[i]<x) continue;
        int cnt=0;
        for(int j=0;j<=t;j++) vis[j]=0;
        while(height[i]>=x&&i<=n-1)
        {
            if(!vis[belong[sa[i-1]]])
            {
                vis[belong[sa[i-1]]]=1;
                cnt++;
            }
            i++;
        }
        if(!vis[belong[sa[i-1]]])
        {
            vis[belong[sa[i-1]]]=1;
            cnt++;
        }
        if(cnt>=t)
        {
            anspos=sa[i-1];
            return true;
        }
    }
    return false;
}

int main(){
    while(~scanf("%d",&t)&&t){
        int len=0,pos=30;
        for(int i=0;i<t;i++){
            scanf("%s",p);
            int l=strlen(p);
            for(int j=0;j<l;j++){
                s[len++]=p[j]-'a'+1;
                belong[len-1]=i;
            }
            s[len++]=pos++;
        }
        s[len]=0;
        getsa(s,sa,len,5000);
        getheight(s,len);
        int l=1,r=200;
        while(l<=r){
            int mid=l+r>>1;
            if(check(mid,len)) l=mid+1;
            else r=mid-1;
        }
        if(r==0) printf("IDENTITY LOST\n");
        else
        {
            for(int i=anspos;i<anspos+r;i++)
                printf("%c",s[i]-1+'a');
            printf("\n");
        }
    }
    return 0;
}

本文标签: POJIDENTITYCorporate