hdu1560DNA sequence(IDA*)

题目描述:

DNA sequence

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


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.

hdu1560DNA sequence(IDA*)
 

 

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 题目大意:有许多序列,要把他们横着排列起来,相同的元素可以排在一行,求最终排列的最少长度 思路:咱们最终排列的长度不知道多长,然后排列的先后顺序也不是很清楚,如果单纯dfs,很可能超时/爆栈, 这时候就需要用到高级的玩意儿,迭代加深搜索,我们可以预设一个搜索的排列长度,我们就再这个长度下 遍历所有情况,如果都不满足就继续加大深度,不怕爆栈而且也不会一条路走到黑,需要注意的是那个用来回溯 的数组一定要再dfs里面定义(别问我怎么知道的...),具体可以看代码注释 AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100;
const int inf = 0x3f3f3f3f;
string c = "ACGT";
string a[maxn];
int pos[10],n;
int getlast() {//所有字符串还没有放置长度的最大值,即剩余需要的step的最小可能值
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans = max(ans, int(a[i].size()) - pos[i]);
    }
    return ans;
}
int depth;
bool dfs(int step) {
    int last = getlast();
    if (step + last>depth)return false;//剩余需要放的长度+已放置了的长度>预设的depth,A*剪枝
    if (!last)return true;//如果没有剩余的需要放了,满足要求
    int tmp[10];//别当成全局变量了...
    memcpy(tmp, pos, sizeof(pos));
    for (int i = 0; i < 4; i++) {//该层放什么元素
        bool f = 0;
        for (int j = 0; j < n; j++) {
            if (pos[j]<int(a[j].size())&&a[j][pos[j]] == c[i]) {
                //如果该字符串下一个放置的字符就是当前遍历的字符,就可以放置
                f = 1;
                pos[j]++;
            }
        }
        if (f) {//如果该层可以放置
            if (dfs(step + 1))return true;
            memcpy(pos, tmp, sizeof(tmp));//回溯
        }
    }
    return false;
}
int main() {
    //freopen("test.txt", "r", stdin);
    int t; scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        int maxlen = 0;
        for (int i = 0; i < n; i++) {
            cin >> a[i]; maxlen = max(maxlen, int(a[i].size()));
            pos[i] = 0;
        }
        depth = maxlen;//预设初始深度为最长的字符串
        while (1) {
            if (dfs(0)) break;
            depth++;
        }
        printf("%d\n", depth);
    }
    return 0;
}
/*
1
4
ACGT
ATGC
CGTT
CAGT
*/
/*
8
*/

 

上一篇:输入一棵二叉树,判断该二叉树是否是平衡二叉树。(剑指offer-平衡二叉树)


下一篇:Node Depths of BinaryTree