前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ--1699 Best Sequence(DP+dfs)

POJ--1699 Best Sequence(DP+dfs)

作者头像
ShenduCC
发布2018-04-25 17:32:52
6470
发布2018-04-25 17:32:52
举报
文章被收录于专栏:算法修养算法修养

Best Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5543 Accepted: 2188 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 之所以说是DP,是因为这道题目实现把每两个字符串连接的状态保存起来,而后进行dfs遍历,最后选出最优值

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>

using namespace std;
char a[11][25];
int  dp[11][11];
int n;
int ans;
int vis[15];
void add(int m,int n)
{
    int k=0;
    int len1=strlen(a[m]);
    int len2=strlen(a[n]);
    bool tag;
    for(int p=1;p<=len1&&p<=len2;p++)
    {
        tag=true;
        for(int i=0,j=len1-p;i<p;i++,j++)
        {
            if(a[m][j]!=a[n][i])
            {
                tag=false;
                break;
            }
        }
        if(tag)
            k=p;
    }
    dp[m][n]=len2-k;
}
void dfs(int pre,int num,int sum)
{

    if(num==n)
    {

        if(ans>sum)
            ans=sum;
        return;
    }
    for(int i=0;i<n;i++)
    {
        if(vis[i]==0)
        {
            vis[i]=1;
            dfs(i,num+1,sum+dp[pre][i]);
            vis[i]=0;
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        getchar();
        for(int i=0;i<n;i++)
            scanf("%s",a[i]);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                add(i,j);
        memset(vis,0,sizeof(vis));
        ans=9999;
        for(int i=0;i<n;i++)
        {
            vis[i]=1;
            dfs(i,1,strlen(a[i]));
            vis[i]=0;
        }
        printf("%d\n",ans);

    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-01-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档