DNA sequence (最长公共子序列)

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.


InputThe 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.OutputFor 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个串中找出一个最短的公共串
#include<iostream>
#include<cstring>
#include<cstdio>
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++)//枚举添加上字符串
    {
            for(int j=0;j<n;j++)//更新point状态
            {
                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]);//sl数组记录每一个字符串长度
            point[i]=0;//将记录数组的这一位初始化
            ans=max(ans,sl[i]);//记录最长的字符串长度
        }
        flag=0;
        while(true)
        {
            dfs(ans);
            if(flag)
                break;
            ans++;//没找到下一次字符串长度++
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2020-11-30 19:34  BlackSnow  阅读(237)  评论(0)    收藏  举报