码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1699:Best Sequence(DP)

时间:2015-04-21 20:49:10      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:dp   poj   

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

Source



与HDU1560是一样的题,那道题我是用IDA*写的,如果直接提交那个代码的话TLE,那么就只能换一种方法做了,我们可以使用dp来优化,dp[i][j],记录第i个字符串和第j个字符串合并后,除掉重合部分,i还剩多少个,那么我们搜索的时候,只需要枚举dp数组的组合方式就可以了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 100005
#define mod 19999997
const int INF = 0x3f3f3f3f;

char str[15][25];
int t,n,dp[15][15],len[15],vis[15];

int cal(int x,int y)
{
    int i,j,sum,flag;
    up(i,0,len[x]-1)
    {
        int ly = 0;
        sum = 0;
        flag = 0;
        up(j,i,len[x]-1)
        {
            if(str[x][j]!=str[y][ly])
            {
                flag = 1;
                break;
            }
            ly++;
            sum++;
        }
        if(!flag)
            return len[x]-sum;
    }
    return len[x];
}

int dfs(int now,int step)
{
    int ans = INF,i;
    if(step == n)
        return len[now];
    up(i,0,n-1)
    {
        if(!vis[i])
        {
            vis[i] = 1;
            int tmp = dp[now][i];
            tmp+=dfs(i,step+1);
            vis[i] = 0;
            ans = min(ans,tmp);
        }
    }
    return ans;
}

int main()
{
    int i,j;
    scanf("%d",&t);
    w(t--)
    {
        mem(dp,0);
        scanf("%d",&n);
        up(i,0,n-1)
        {
            scanf("%s",str[i]);
            len[i] = strlen(str[i]);
        }
        up(i,0,n-1)
        {
            up(j,0,n-1)
            {
                if(i==j) continue;
                dp[i][j] = cal(i,j);
            }
        }
        mem(vis,0);
        int ans = INF;
        up(i,0,n-1)
        {
            vis[i] = 1;
            ans = min(ans,dfs(i,1));
            vis[i] = 0;
        }
        printf("%d\n",ans);
    }

    return 0;
}


POJ1699:Best Sequence(DP)

标签:dp   poj   

原文地址:http://blog.csdn.net/libin56842/article/details/45174575

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!