hdu 1560 DNA sequence(迭代加深搜索)

DNA sequence

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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.

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

Author

LL

Source

HDU 2006-12 Programming Contest

使用dfs进行搜索,但限制递归深度。

逐步加深搜索深度,直至找到答案。

主函数中, 限制搜索深度,如果无解,就加深1层深度

强力剪枝: 递归函数中, 首先计算最坏情况下,还需要补充长度:

为每个DNA序列还未匹配的长度之和(sum)。

如果现在搜索深度+sum>限定的搜索深度,则返回

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char f[4]={‘A‘,‘T‘,‘G‘,‘C‘};
int flag,i,t,n,maxlen;
int cnt[50];
char str[10][10];
void dfs(int len,int cnt[])
{
    if (flag || len>maxlen) return;

    int sum=0;
    for(int i=0;i<n;i++)  //关键 :ida*(迭代加深搜索)
        {
           int l=strlen(str[i]);
            sum=max(sum,l-cnt[i]);
        }
    if (sum+len>maxlen) return;
    if (sum==0) {flag=1; return;}

    for(int i=0;i<4;i++)
    {
        char x=f[i];
        int next[50];
        int tflag=0;
        for(int j=0;j<n;j++)
          if (str[j][cnt[j]]==x)
        {
            next[j]=cnt[j]+1;
            tflag=1;
        } else next[j]=cnt[j];
       if (tflag) dfs(len+1,next);  //更新了才说明有效
    }
    return;
}
int main()
{
    scanf("%d",&t);
    for(;t>0;t--)
    {
        scanf("%d",&n);
        maxlen=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            int l=strlen(str[i]);
            maxlen=max( maxlen,l );
        }
        flag=0;
        memset(cnt,0,sizeof(cnt));
        for(i=0;i<40;i++)
        {
            dfs(0,cnt);
            if (flag) break;
            maxlen++;
        }
        printf("%d\n",maxlen);
    }
    return 0;
}
时间: 2024-08-10 07:21:07

hdu 1560 DNA sequence(迭代加深搜索)的相关文章

HDU 1560 DNA sequence(DNA序列)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

HDU - 1560 DNA sequence

给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<

HDU 1560 DNA sequence(IDA*)

DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6042    Accepted Submission(s): 2735 Problem Description The twenty-first century is a biology-technology developing century. We kno

HDU 1560 DNA sequence A* 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=1560 仔细读题(!),则可发现这道题要求的是一个最短的字符串,该字符串的不连续子序列中包含题目所给的所有字符串 因为总共只有40个字符,可以尝试使用A*搜索 1.存储状态时直接存储40个字符,每个字符4种可能是肯定不行的. 因为要求的是包含不连续的子序列,所以只需记住当前字符串长度与每个子序列已经包含在当前字符串的长度, 比如题目中的输入样例 4 ACGT ATGC CGTT CAGT 可以这样存储一个序列

hdu 1560 迭代加深搜索

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 只能说bin神太给力了.. 又学到不少新知识.. 迭代加深搜索,貌似 又叫IDA*, 就是给搜索深度一个限制,搜索到一个满足条件就结束. 注意剪枝~ 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; char g[10][10]; int size[10];

hdu 2485 Destroying the bus stations 迭代加深搜索

求最少去掉几个公交站使得从s到t的最短路径大于k. 迭代加深搜索. #include <cstdio> #include <cstring> #include <queue> using namespace std; #define maxn 60 #define maxm 50000 int n,m,K,cnt,up; int head[maxn],pre[maxn]; int road[maxn][maxn]; bool del[maxn]; queue<in

HDOJ 1560 DNA sequence 状压dp 或 IDA*

http://acm.hdu.edu.cn/showproblem.php?pid=1560 题意: 给不超过8个子串,每个子串最多5位,且都只包含ATCG,求最短的母串长度. 分析: 又是上个月写的,所以有点忘了..正解是IDA*.然后可以状压dp,记忆化搜索.dp[i],i用6进制表示,每位表示对应的子串匹配那么多长度所需要的最短母串长度.比如两个子串,13=2*6^1+1*6^0,dp[13]就表示第一个串匹配了第一位,第二个串匹配前两位所需要的最短母串长度. 状态讲完了,不过实际上程序里

HDU1560(迭代加深搜索)

DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1914    Accepted Submission(s): 946 Problem Description The twenty-first century is a biology-technology developing century. We know

poj3009——迭代加深搜索

POJ 3009  迭代加深搜索 Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12986   Accepted: 5460 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from o