HDU 1560 - DNA sequence

E - DNA sequence
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

The twenty-first century is a biology-technology developing century. We know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Finding the longest common subsequence between DNA/Protein sequences is one of the basic problems in modern computational molecular biology. But this problem is a little different. Given several DNA sequences, you are asked to make a shortest sequence from them so that each of the given sequence is the subsequence of it. 

For example, given "ACGT","ATGC","CGTT" and "CAGT", you can make a sequence in the following way. It is the shortest but may be not the only one. 

 

Input

The first line is the test case number t. Then t test cases follow. In each case, the first line is an integer n ( 1<=n<=8 ) represents number of the DNA sequences. The following k lines contain the k sequences, one per line. Assuming that the length of any sequence is between 1 and 5.
 

Output

For each test case, print a line containing the length of the shortest sequence that can be made from these sequences.
 

Sample Input

1 4 ACGT ATGC CGTT CAGT
 

Sample Output

8

IDA*,temp[]記錄每個字符串搜索到第幾位


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;

#define N 10

struct node{
    int len;
    string s;
}a[N];

int n;
int pos[N],depth,ans;
char dna[]="ACGT";

int calen(int *s){
    int m=0;
    for (int i=0;i<n;i++)
        m=max(m,a[i].len-s[i]);
    return m;
}

bool dfs(int *s,int now){
    if (now+calen(s)>depth)
        return false;
    if (calen(s)==0)
        return true;

    int temp[N];

    for (int i=0;i<4;i++){
        bool flag=false;
        memcpy(temp,s,sizeof(temp));
        for (int j=0;j<n;j++){
            if (a[j].s[temp[j]]==dna[i]){
                flag=true;
                temp[j]++;
            }
        }

        if (flag){
            if (dfs(temp,now+1))
                return true;
        }
    }
    return false;
}

int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        scanf("%d",&n);
        for (int i=0;i<n;i++){
            cin>>a[i].s;
            a[i].len=a[i].s.length();
        }
        memset(pos,0,sizeof(pos));
        depth=1;
        while (1){
            if (dfs(pos,0))
                break;
            depth++;
        }

        printf("%d\n",depth);
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章