zoukankan      html  css  js  c++  java
  • Blue Jeans

    大致题意:

        给出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]='';
     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
    ",res);
     99         }else{
    100             printf("no significant commonalities
    ");
    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
  • 相关阅读:
    图像边界扩展
    数据增强之图像旋转及坐标对应(附代码)
    Opencv中的阈值函数
    linux下C++遍历文件夹下的全部文件;Windows/Linux下C++批量修改文件名,批量删除文件
    提取内环图像
    linux中tab键不能补全,却能切换窗口
    Mathtype部分数学符号不能显示,只能显示方框时的解决办法
    Qt运行不出现界面
    直方图处理
    DTCMS自定义标签:面包屑导航,栏目中通过栏目调用名称获得栏目名称
  • 原文地址:https://www.cnblogs.com/sdxk/p/4685610.html
Copyright © 2011-2022 走看看