HDU-1560 DNA sequence (迭代深搜)

来源:互联网 发布:ajax怎么解析json数据 编辑:程序博客网 时间:2024/06/09 18:53

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

嗯,开始直接暴力枚举的各个字符串顺序,有点蠢。。。题意理解有点偏。

利用point指针数组来记录状态。

可以枚举长度,枚举4个字符。

然后利用check的剪枝。

对于当前你已经利用的字符个数n,可以实现匹配的状态是point[i]那么,

对于剩余的字符个数sum和每个字符串剩余的字符数,就是一个上面的子问题,我们可以递归解决。

直到所有字符都可以匹配,这样就找到解了。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int MAXN = 10;const int inf = 1e9;int n,m;//记录字符串char s[MAXN][MAXN];//记录字符串的长度int sl[MAXN];//记录字符串当前匹配到哪了int point[MAXN];char DNA[]="AGCT";int check(){    int res = 0;    for(int i = 0; i < n; ++i)res = max(res,sl[i] - point[i]);    return res;}int flag;void dfs(int sum){    int c = check();    //如果找到了解    if(!c)    {        flag = 1;        return;    }    //当前不可能符合条件    if(sum < c)return;    //保存现场    int temp[10];    for(int i = 0; i < n; ++i)temp[i] = point[i];    //搜索开始    //表示是否有变化    bool ok = 0;    //枚举添加上的字符    for(int i = 0; i < 4; ++i)    {        //更新point的状态        for(int j = 0; j < n; ++j)        {            if(s[j][point[j]] == DNA[i])            {                point[j]++;                ok = 1;            }        }        //如果更新了        if(ok)        {            //继续枚举下一个字符            dfs(sum-1);            if(flag)return;            //恢复现场            for(int j = 0 ;j < n; ++j)point[j] = temp[j];        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        int ans = 0;        for(int i = 0; i < n; ++i)        {            scanf("%s",s[i]);            sl[i] = strlen(s[i]);            point[i] = 0;            ans = max(ans,sl[i]);        }        flag = 0;        while(1)        {            dfs(ans);            if(flag)break;            ans++;        }        printf("%d\n",ans);    }    return 0;}