hdu 1560 DNA sequence(IDA*)

DNA sequence

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3872    Accepted Submission(s): 1870


 

Problem 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*,估值函数为A,C,G,T四个字母分别在每个字符串中出现
最多的一次的的次数和 与 最长字符串比较的最大值

代码:

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
char t[10][10],op[4]={'A','C','G','T'};
int n, st[10], en[10];
int s[404];
int f()
{
    s['A']=s['C']=s['G']=s['T']=0;
    int len=0;int vis[404];
    for(int i=0;i<n;i++)
    {
        vis['A']=vis['C']=vis['G']=vis['T']=0;
        for(int j=st[i];j<en[i];j++)
            vis[t[i][j]]++;
        s['A']=max(s['A'],vis['A']);
        s['C']=max(s['C'],vis['C']);
        s['G']=max(s['G'],vis['G']);
        s['T']=max(s['T'],vis['T']);
        len=max(len,en[i]-st[i]);
    }
    return max(len,s['A']+s['C']+s['G']+s['T']);
}
bool dfs(int step, int upper)
{
    int mht = f();
    if (mht == 0) return 1;
    if (mht + step >upper) return 0;
    for(int i=0;i<4;i++)
    {
        bool vis[10],bb=0;
        memset(vis,0,sizeof(vis));
        for(int j=0;j<n;j++)
            if(st[j]<en[j]&&t[j][st[j]]==op[i])
            st[j]++,vis[j]=1,bb=1;
        if(bb)
        {
            if(dfs(step+1,upper))
                return 1;
        }
        for(int j=0;j<n;j++)
            if(vis[j]) st[j]--;
    }
    return 0;
}
int main()
{
    int T; scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        for (int i = 0; i < n; i++)
            scanf("%s",&t[i]);
        for (int i = 0; i<n; i++)
        {
            st[i] = 0;
            en[i] = strlen(t[i]);
        }
        int top = f();
        while (!dfs(0, top))top++;
        printf("%d\n",top);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/81092787