HDU 1506 DNA sequence IDA*

来源:互联网 发布:原油数据影响大吗 编辑:程序博客网 时间:2024/06/05 18:02
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
14ACGTATGCCGTTCAGT
Sample Output
8

 迭代加深搜索(IDA*)

#include<iostream>//迭代加深搜索IDA*#include<cstdio>#include<cstring>using namespace std;struct node{    int p[10];//经过迭代产生的长度}temp;int n,len[12];char s[12][12];char DNA[]="ACGT";int DFS(node tt,int ss,int d)//ss为迭代深度d为当前深度{    struct node f;    if(ss>d) return 0;//迭代深度超过d    int i,j;    for(i=0;i<=n-1;i++)    {        if(len[i]-tt.p[i]+ss>d) return 0;//未迭代的长度加上迭代深度如果大于d返回0    }    for(i=0;i<=n-1;i++)    {        if(tt.p[i]<len[i]) break;    }    if(i==n) return 1;//全部迭代完成    int flag;    for(i=0;i<=3;i++)//匹配ACGT    {        flag=0;        for(j=0;j<=n-1;j++)        {            if(s[j][tt.p[j]]==DNA[i])            {                flag=1;//表示匹配上                f.p[j]=tt.p[j]+1;            }            else            {                f.p[j]=tt.p[j];            }        }        if(flag&&DFS(f,ss+1,d)) return 1;//ss+1,递归迭代下一层    }    return 0;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        int i;        for(i=0;i<=n-1;i++)        {            scanf("%s",s[i]);            len[i]=strlen(s[i]);        }        for(i=0;;i++)//i为迭代加深的深度            if(DFS(temp,0,i)) break;//从ss=0开始迭代搜索,到i结束        printf("%d\n",i);//输出深度,即最短的字符串长度    }    return 0;}


原创粉丝点击