POJ1699:Best Sequence(DP)

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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值