题面
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
参考链接
题意简述
求包含题目所给n个序列的序列的最短长度
用BFS做这道题,妥妥地MLE了
初学IDA*
get_h()计算估价函数,即时退出
程序
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int n,deep;/**deep即长度**/
char c[]="ACGT";
struct Node
{
char s[10];
int len;
}node[10];
int where[10];/**第i个序列已经匹配where[i]个**/
int get_h()/**估价函数|此时还需的最大长度**/
{
int answer=0;
for(int i=1;i<=n;i++)
answer=max(answer,node[i].len-where[i]);
return answer;
}
bool dfs(int step)/**已有长度**/
{
if(step+get_h()>deep)
return false;
if(get_h()==0)/**匹配完成**/
return true;
int temp[10];/**临时保存where数组**/
for(int i=0;i<4;i++)
{
bool flag=false;
for(int j=1;j<=n;j++)
temp[j]=where[j];/**把where保存下来**/
for(int j=1;j<=n;j++)
{
if(node[j].s[where[j]]==c[i])
{
flag=true;
where[j]++;
}
}
if(flag)
{
if(dfs(step+1))
return true;
for(int j=1;j<=n;j++)
where[j]=temp[j];
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int max_len=0;
memset(where,0,sizeof(where));
for(int i=1;i<=n;i++)
{
scanf("%s",node[i].s);
node[i].len=strlen(node[i].s);
max_len=max(max_len,node[i].len);
}
deep=max_len;
while(1)
{
if(dfs(0))
break;
deep++;
}
printf("%d\n",deep);
}
return 0;
}