码迷,mamicode.com
首页 > 编程语言 > 详细

Blue Jeans - poj 3080(后缀数组)

时间:2015-07-31 10:29:22      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

大致题意:

    给出n个长度为60的DNA基因(A腺嘌呤 G鸟嘌呤 T胸腺嘧啶 C胞嘧啶)序列,求出他们的最长公共子序列

使用后缀数组解决

  1 #include<stdio.h>
  2 #include<string.h>
  3 char str[6200],res[6200];
  4 int num[6200],loc[6200];
  5 int sa[6200],rank[6200],height[6200];
  6 int wa[6200],wb[6200],wv[6200],wd[6200];
  7 int vis[6200];
  8 int seq_num;
  9 int cmp(int *r,int a,int b,int l){
 10     return r[a]==r[b]&&r[a+l]==r[b+l];
 11 }
 12 void DA(int *r,int n,int m){
 13     int i,j,p,*x=wa,*y=wb,*t;
 14     for(i=0;i<m;i++)wd[i]=0;
 15     for(i=0;i<n;i++)wd[x[i]=r[i]]++;
 16     for(i=1;i<m;i++)wd[i]+=wd[i-1];
 17     for(i=n-1;i>=0;i--) sa[--wd[x[i]]]=i;
 18     for(j=1,p=1;p<n;j*=2,m=p){
 19         for(p=0,i=n-j;i<n;i++) y[p++]=i;
 20         for(i=0;i<n;i++) if(sa[i]>=j) y[p++] = sa[i] -j;
 21         for(i=0;i<n;i++)wv[i]=x[y[i]];
 22         for(i=0;i<m;i++) wd[i]=0;
 23         for(i=0;i<n;i++)wd[wv[i]]++;
 24         for(i=1;i<m;i++)wd[i]+=wd[i-1];
 25         for(i=n-1;i>=0;i--) sa[--wd[wv[i]]]=y[i];
 26         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++){
 27             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
 28         }
 29     }
 30 }
 31 void calHeight(int *r,int n){
 32     int i,j,k=0;
 33     for(i=1;i<=n;i++)rank[sa[i]]=i;
 34     for(i=0;i<n;height[rank[i++]]=k){
 35         for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
 36     }
 37 }
 38 int check(int mid,int len){
 39     int i,j,tot;
 40     tot=0;
 41     memset(vis,0,sizeof(vis));
 42     for(i=2;i<=len;i++){
 43         if(height[i]<mid){
 44             memset(vis,0,sizeof(vis));
 45             tot=0;
 46         }else{
 47             if(!vis[loc[sa[i-1]]]){
 48                 vis[loc[sa[i-1]]]=1;
 49                 tot++;
 50             }
 51             if(!vis[loc[sa[i]]]){
 52                 vis[loc[sa[i]]]=1;
 53                 tot++;
 54             }
 55             if(tot==seq_num){
 56                 for(j=0;j<mid;j++){
 57                     res[j]=num[sa[i]+j]+A-1;
 58                 }res[mid]=\0;
 59                 return 1;
 60             }
 61         }
 62     }
 63     return 0;
 64 }
 65 int main() {
 66     int case_num,n,sp,ans;
 67     scanf("%d",&case_num);
 68     for(int i=0;i<case_num;i++){
 69         scanf("%d",&seq_num);
 70         n=0;
 71         sp=29;
 72         ans=0;
 73         for(int j=0;j<seq_num;j++){
 74             scanf("%s",str);
 75             for(int k=0;k<60;k++){
 76                 loc[n]=j;
 77                 num[n++]=str[k]-A+1;
 78             }
 79             loc[n]=sp;
 80             num[n++]=sp++;
 81         }
 82         num[n]=0;
 83         DA(num,n+1,sp);
 84         calHeight(num,n);
 85         int left=0,right=60,mid;
 86 
 87         while(right>=left){
 88             mid=(right+left)/2;
 89             int tt=check(mid,n);
 90             if(tt){
 91                 left=mid+1;
 92                 ans=mid;
 93             }else{
 94                 right=mid-1;
 95             }
 96         }
 97         if(ans>=3){
 98             printf("%s\n",res);
 99         }else{
100             printf("no significant commonalities\n");
101         }
102     }
103     return 0;
104 }

 

 
附:原题目
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14020   Accepted: 6227

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

Blue Jeans - poj 3080(后缀数组)

标签:

原文地址:http://www.cnblogs.com/sdxk/p/4685610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!