【题目描述】
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.
21世纪是一个生物技术发展的世纪。我们知道基因是由DNA构成的。构建DNA的核苷酸碱基是A(腺嘌呤)、C(胞嘧啶)、G(鸟嘌呤)和T(胸腺嘧啶)。寻找DNA/蛋白质序列之间最长的公共子序列是现代计算分子生物学的基本问题之一。但是这个问题有点不同。给定几个DNA序列,你被要求从它们中得到一个最短的序列,这样每个给定的序列就是它的子序列。
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.
例如,给定“ACGT”、“ATGC”、“CGTT”和“CAGT”,您可以通过以下方式生成序列。它是最短的,但可能不是唯一的。
【输入】
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.
第一行是测试样例组数。后面是t个测试样例。
在每个测试样例中,第一行是一个整数n(1<=n<=8)代表DNA序列的数量。后面k行包括k个序列,每个一行。假设任何一个序列的长度都在1到5之间。
【输出】
For each test case, print a line containing the length of the shortest sequence that can be made from these sequences.
对于每个测试样例,输出一行包括能够组成这些序列的最短序列的长度。
【样例输入】
1
4
ACGT
ATGC
CGTT
CAGT
【样例输出】
8
题目链接:https://cn.vjudge.net/problem/HDU-1560
读完题的第一反应竟然是BFS,还是太菜了,爆了一万次内存看了别人的代码才反应过来
迭代加深DFS+剪枝
代码如下:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
static const int MAXN=8;
static const int MAXL=5;
static string DNA="ACGT";
int T,n,deep;
char s[MAXN+2][MAXL+2];
int pos[MAXN+2];
int len[MAXN+2];
int need()
{
int maxneed=0;
for(int i=0;i<n;i++)
maxneed=max(maxneed,len[i]-pos[i]);
return maxneed;
}
int dfs(int step)
{
if(step+need()>deep)
return 0;
if(need()==0)
return 1;
int pre[MAXN+2];
for(int i=0;i<4;i++)
{
int flag=0;
for(int j=0;j<n;j++)
{
pre[j]=pos[j];
if(s[j][pos[j]]==DNA[i])
{
pos[j]++;
flag=1;
}
}
if(flag)
{
if(dfs(step+1))
return 1;
for(int i=0;i<n;i++)
pos[i]=pre[i];
}
}
return 0;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
cin>>T;
while(T--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s[i];
len[i]=strlen(s[i]);
}
memset(pos,0,sizeof(pos));
deep=0;
while(1)
if(dfs(0))
{
cout<<deep<<endl;
break;
}
else
deep++;
}
return 0;
}