DNA sequence

DNA sequence

问题链接

http://acm.hdu.edu.cn/showproblem.php?pid=1560
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

问题分析

从n个串中找出一个最短的公共串。
方法从别人那学来的:迭代加深搜索,要限制搜索的深度(剪枝)。

AC代码

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,h;
char str[10][10],a[4]= {'A','C','G','T'};
int len[9],c[9];
int dfs(int b[],int s,int deep)
{
    int i,j,k,d[9];
    for(i=0; i<n; i++)
    {
        if(len[i]-b[i]+s>deep) return 0;//剪枝
    }
    for(i=0; i<n; i++)
    {
        if(str[i][b[i]]) break;
    }
    if(i==n) return 1;
    for(i=0; i<4; i++)
    {
        for(j=k=0; j<n; j++)
        {
            d[j]=b[j];
            if(a[i]==str[j][b[j]])
			 d[j]+=1,k=1;
        }
        if(k!=0)
            if(dfs(d,s+1,deep)) return 1;
    }
    return 0;
}
int main()
{
    int i,k;
    cin>>h;
    while(h--)
    {
        cin>>n;
        for(i=k=0; i<n; i++)
        {
            cin>>str[i];
            len[i]=strlen(str[i]);
        }
        memset(c,0,sizeof(c));
        while((dfs(c,0,k))==0)
            k++;
        cout<<k<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43993717/article/details/86669885