标签:
大致题意:
给出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
Input
Output
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
标签:
原文地址:http://www.cnblogs.com/sdxk/p/4685610.html