poj3080 后缀数组+二分查找

(http://www.elijahqi.win/2017/07/19/poj3080-%E5%90%8E%E7%BC%80%E6%95%B0%E7%BB%84%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/)
Blue Jeans
Time Limit: 1000MS

Memory Limit: 65536K
Total Submissions: 18245

Accepted: 8094
Description
The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated.

As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers.

A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC.

Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.
Input
Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
m lines each containing a single base sequence consisting of 60 bases.
Output
For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string “no significant commonalities” instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.
Sample Input
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities
AGATAC
CATCATCAT
Source
South Central USA 2006
其实这题是同Poj3450的 但是我 对于每一个没有处理好,调试了很久。。
这里补充说明一些二分查找的地方,早晨查错的时候发现poj3450的二分查找应该是有bug的 可能当答案就是60的时候答案会得出59相比少一个。
此外 check程序,完善一下为什么不判断sa[i-1]因为假如height不满足的时候,我们已经把他加入visit所以没有必要再判断

注意 ans<3时输出no significant commonalities

#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("poj3080.in","r",stdin);
    freopen("poj3080.out","w",stdout);
    int nnn;
    scanf("%d",&nnn);
    while (nnn--){
        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-1;
        }
        if (ans<3) printf("no significant commonalities\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、付费专栏及课程。

余额充值