poj 1699—Best Sequence(dfs剪枝)

http://poj.org/problem?id=1699

Description

The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently 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). Given several segments of a gene, you are asked to make a shortest sequence from them. The sequence should use all the segments, and you cannot flip any of the segments. 

For example, given 'TCGG', 'GCAG', 'CCGC', 'GATC' and 'ATCG', you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one). 

Input

The first line is an integer T (1 <= T <= 20), which shows the number of the cases. Then T test cases follow. The first line of every test case contains an integer N (1 <= N <= 10), which represents the number of segments. The following N lines express N segments, respectively. Assuming that the length of any segment is between 1 and 20.

Output

For each test case, print a line containing the length of the shortest sequence that can be made from these segments.

Sample Input

1
5
TCGG
GCAG
CCGC
GATC
ATCG

Sample Output

11
题目大意:有一些DNA序列,通过连接组合,组成最短的序列,如果一个序列的头部和另一个序列的尾部的字符串相同的话,那么这相同的一部分算一个长度,如图所示

基本思路:遍历每一个字符串,另一个字符串作为第一个,然后dfs找最优的,稍作剪枝,因为序列的最长度是确定的,切如果找出一个比较小的后,找dfs时比这个数小的就不用在遍历了

在这里可以预先处理一下任意两个字符串相连接时所增加的长度

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

int len[15];
int vis[15];
char s[15][250];

int addlen1[15][15];
int n;
int ans;
void add1(int x,int y)
{
//    cout<<"----------------------"<<endl;
//    cout<<"**"<<s[x]<<endl<<s[y]<<endl;
    int v=len[x];
    for(int i=0;i<len[x];i++)
    {
        int j=i,k=0;
        while(j<len[x]&&k<len[y])
        {
            if(s[x][j]!=s[y][k])break;
            j++;
            k++;
        }
        if(j>=len[x])
        {
            v=i;
            break;
        }
    }
    int d=len[x]-v;
    int ad=len[y]-d;
    if(ad<0)ad=0;
    addlen1[x][y]=ad;
    //cout<<ad<<endl;
}

void dfs(int x,int d,int sum)
{
    if(d>=ans)return ;///剪枝
    if(sum==n)
    {
        //cout<<"d = "<<d<<endl;
        if(d<ans)ans=d;
        return ;
    }
    for(int i=0; i<n; i++)
    {
        if(vis[i]==0)
        {
            vis[i]=1;
            dfs(i,d+addlen1[x][i],sum+1);
            vis[i]=0;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%s",s[i]);
            len[i]=strlen(s[i]);
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                add1(i,j);
            }
        }
        ans=20*10+10;
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++) ///以第i个为起点
        {
            vis[i]=1;
            dfs(i,len[i],1);
            vis[i]=0;
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值