poj3450 Corporate Identity(后缀数组+二分答案)

(http://www.elijahqi.win/2017/07/18/poj3450-corporat%E2%80%A6entity%EF%BC%88%E5%90%8E%E7%BC%80%E6%95%B0%E7%BB%84%E4%BA%8C%E5%88%86%E7%AD%94%E6%A1%88%EF%BC%89/)
Corporate Identity
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 7105 Accepted: 2505

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

Source
CTU Open 2007
今天在旅途中敲完了整体的代码 回家花了几个小时查错..
这道题 后缀数组确实不是正解 我的时间29xxms 差一点就被卡了的 主要倍增写的不好
注意:1、在倍增算法中,清0,计算基数排序柱子的时候注意都需要限制范围,优化时间
2、二分的时候,注意答案的范围应该是字符串中长度最小的那个而非最后中间加入特殊字符构造出来的n
新增设一个数组id[]表明这个后缀属于第几个
一开始写二分的时候楞了一下,半天竟然没想出来
其实就是对height进行处理,如果height都满足我们枚举的这个答案,那么统计一下我们都有几个满足条件的后缀属于不同的字符串,如果等于输入的k1那么就退出,如果中断了,要把我们的visit都清0 因为根据height的定义,height一定是最近的,最长的,关于为什么没有判断visit[id[sa[i-1]]] ,因为最后for循环一定把所有的后缀都会循环,判断次数 应该是近乎相同的

#include<cstdio>
#include<cstring>
#define N 880000
#define N1 4400
int k1,n,id[N],ans,ans1;
char str1[220];
int a[N],st[N1],rank[N<<1],rank1[N],count[N],tmp[N],sa[N],height[N];
inline int min(int x,int y){
    return x<y?x:y;
}
bool visit[N1];
inline bool check(int x){
    int tmp=0;
    for (int i=1;i<=n;++i){
        if (height[i]>=x){
            if (!visit[id[sa[i]]]) tmp++,visit[id[sa[i]]]=true;
        }else{
            //if (tmp==0) continue;
            memset(visit,false,sizeof(visit));
            tmp=visit[id[sa[i]]]=1;
        }
        if (tmp==k1) {
            ans1=sa[i];return true; 
        }
    }
    return false;
}
int main(){
    freopen("poj3450.in","r",stdin);
    freopen("poj3450.out","w",stdout);
    while (1){
        memset(a,0,sizeof(a));
        scanf("%d",&k1);if (k1==0) return 0;
        int r1=30+k1;
        n=0;int min1=330;
        for (int i=1;i<=k1;++i){
            scanf("%s",str1);
            int n1=strlen(str1);min1=min(min1,n1);
            for (int j=n+n1;j>n;--j){
                a[j]=str1[j-n-1]-'a'+1;id[j]=i;
            }
            n+=(n1+1);
            a[n]=30+i;
        }
    //  for (int i=1;i<=n;++i) printf("%d ",a[i]);
        //printf("\n");
        //getrank&sa&height
    //  memset(st,0,sizeof(st));
        for (int i=1;i<=r1;++i) st[i]=0;
        memset(rank,0,sizeof(rank));
        //memset(rank1,0,sizeof(rank1));
        for (int i=1;i<=n;++i) st[a[i]]=1;
        for (int i=1;i<=r1;++i) st[i]+=st[i-1];
        for (int i=1;i<=n;++i) rank[i]=st[a[i]];
    //  for (int i=1;i<=n;++i) printf("%d ",rank[i]);printf("\n");
        int k=0;
        for (int p=1;k!=n;p+=p,r1=k){
            for (int i=1;i<=r1;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i+p]]++;
            for (int i=1;i<=r1;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
            for (int i=1;i<=r1;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i]]++;
            for (int i=1;i<=r1;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) sa[count[rank[tmp[i]]]--]=tmp[i];
            memcpy(rank1,rank,sizeof(rank)>>1);
            rank[sa[1]]=k=1;
            for (int i=2;i<=n;++i){
                if (rank1[sa[i]]!=rank1[sa[i-1]]||rank1[sa[i]+p]!=rank1[sa[i-1]+p]) ++k;
                rank[sa[i]]=k;
            }
        } 
        //for (int i=1;i<=n;++i) printf("%d ",rank[i]);printf("\n");
        k=0;
        for (int i=1;i<=n;++i){
            if (rank[i]==1){
                height[1]=0;continue;
            }
            k=k==0?0:k-1;
            while (a[i+k]==a[sa[rank[i]-1]+k]) ++k;
            height[rank[i]]=k;
        }
        //for (int i=1;i<=n;++i) printf("%d ",height[i]);printf("\n");
        int l=1,r=min1;ans=ans1=0;
        while (l<r){
            int mid=(l+r)>>1;
            if (check(mid)) ans=mid,l=mid+1;else r=mid;
        }
        if (l==1) printf("IDENTITY LOST\n");else {
            for (int i=0;i<ans;++i) putchar(a[ans1+i]+'a'-1);
            printf("\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值