poj3294 后缀数组+二分

(http://www.elijahqi.win/2017/07/19/poj3294-%E5%90%8E%E7%BC%80%E6%95%B0%E7%BB%84%E4%BA%8C%E5%88%86/)
Life Forms
Time Limit: 5000MS

Memory Limit: 65536K
Total Submissions: 15713

Accepted: 4615
Description
You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.
The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant’s life forms ended up with a large fragment of common DNA.
Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.
Input
Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.
Output
For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output “?”. Leave an empty line between test cases.
Sample Input
3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0
Sample Output
bcdefg
cdefgh

?
Source
Waterloo Local Contest, 2006.9.30
给定n个字符串,求出现在不小于k/2个字符串中的最长子串。
将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组。(何琦后缀数组需要优化)
然后二分答案,将后缀分成若干组,判断每组的后缀是否出现在不小于k个的原串中。这个做法的时间复杂度为O(nlogn)。
二分答案的时候由于下一次check的时候有可能更新这一次亦有可能不更新这一次,所以我们另外加设一个数组储存,一开始时数组开小了RE 我以为时rank和queue冲突了,改掉queue 用了两个q,q1数组存储答案的起点,最后从起点输出ans长度即可
另外贡献一组数据
IN:
2
adsf
asdf
2
fdsa
df
5
abcd
bcde
cdef
defg
efgh
5
nshiyedashai
goanshab
woshidiyi
firstorloser
iamthefirst
4
acaaaaaaaaaaaaa
aaaaa
aa
a
4
aa
a
d
d
2
pabcp
pertp
0

OUT:
a
d
f
s

d
f

cd
de
ef

sh

aa

?

p
注意输出格式

#include<cstdio>
#include<cstring>
#define N 110000
char str1[1100];
int a[N],id[N];
int k,n,l,r,m,k1,count[N],rank[N<<1],rank1[N],tmp[N],sa[N],height[N],ans;
inline int max(int x,int y){
    return x>y?x:y;
}
int q[N],n2,q1[N],n3;
inline bool check(int x){
    bool visit[110],flag=false,flag1=false;int cnt=0;
    n2=0;
    for (int i=1;i<=n;++i){
        if (height[i]>=x){
            if (!visit[id[sa[i]]]) visit[id[sa[i]]]=true,cnt++;
        }else{
            memset(visit,false,sizeof(visit));
            visit[id[sa[i]]]=true;cnt=1;flag=false;
        }
        if ((cnt>(k1>>1))&&(!flag)) q[++n2]=sa[i],flag=true,flag1=true;
    }
    if (flag1) {
        for (int i=1;i<=n2;++i) q1[i]=q[i];n3=n2;
    }
    return flag1;
}
int main(){
    //freopen("poj3294.in","r",stdin);
    //freopen("poj3294.out","w",stdout);
    while(~scanf("%d",&k1),k1){
        n=0;m=30+k1;r=0;l=1;n2=0;
        for (int i=1;i<=k1;++i){
            scanf("%s",str1);
            int n1=strlen(str1);r=max(r,n1);
            for (int j=n+n1;j>n;--j) a[j]=str1[j-n-1]-'a'+1,id[j]=i;
            n=n+n1;
            a[++n]=30+i;id[n]=i;
        }
        //for (int i=1;i<=n;++i) printf("%d ",a[i]);printf("\n");
        for (int i=1;i<=m;++i) count[i]=0;
        memset(rank,0,sizeof(rank));
        for (int i=1;i<=n;++i) count[a[i]]=1;
        for (int i=1;i<=m;++i) count[i]+=count[i-1];
        for (int i=1;i<=n;++i) rank[i]=count[a[i]];
        k=0;
        for (int p=1;k!=n;p<<=1,m=k){
            for (int i=1;i<=m;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i+p]]++;
            for (int i=1;i<=m;++i) count[i]+=count[i-1];
            for (int i=n;i>=1;--i) tmp[count[rank[i+p]]--]=i;
            for (int i=1;i<=m;++i) count[i]=0;
            for (int i=1;i<=n;++i) count[rank[i]]++;
            for (int i=1;i<=m;++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-1]]!=rank1[sa[i]]||rank1[sa[i-1]+p]!=rank1[sa[i]+p])++k;
                rank[sa[i]]=k;
            }
        }
        k=0;
        for (int i=1;i<=n;++i){
            if (rank[i]==0){
                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");
        while (l<=r){
            int mid=(l+r)>>1;
            if (check(mid)) ans=mid,l=mid+1;else r=mid-1;
            //if (l<r) n2=0;
        }

        if (l==1)printf("?\n");else{
            for (int j=1;j<=n3;++j){
                int tmp1=q1[j];
                for (int i=tmp1;i<ans+tmp1;++i) putchar(a[i]+'a'-1);
                puts("");
            }
        }
        printf("\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值