poj 1699 Best Sequence(dfs+A(n,n))

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

Best Sequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5467 Accepted: 2146

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
分析:已知多个片段个体求极值问题,想到了动态规划。根据题意,两个字符串组合,如s1=“12345”,s2=“345678”新构成的字符串的长度比原来s1的长度要多3,把增加的长度存储在一个二维数组len[1][2]中。这样,对于以字符串si开头的由n个子串组合成的新的字符串的就是si+len(i,j,k,……)[ A(n-1,n-1)种组合情况]。那么怎样模拟这么多的组合情况呢?深度优先搜索。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int getmore(char *s1,char *s2){
    int length1=strlen(s1),length2=strlen(s2),i;
    int m,qi;
    if(length1>length2)qi=length1-length2;
    else qi=0;
    for(;qi<length1;qi++){
         bool judge=1;
         for(int i=qi;i<length1;i++){
              if(s1[i]!=s2[i-qi])judge=0;
         }
         if(judge)break;
    }
    return length2-length1+qi;
}
int len[11][11],length[11],N;
char str[11][25];
int ans=INF;
bool used[11];
void dfs(int dex,int length,int count){
    if(length>ans)return ;
    if(count==N && length<ans){  ans=length; return ; }
    for(int i=0;i<N;i++){
        if(used[i])continue;
        used[i]=1;
        dfs(i,length+len[dex][i],count+1);
        used[i]=0;
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int t;
    cin>>t;
    while(t--){
         scanf("%d",&N);
         ans=INF;
         memset(length,0,sizeof(length));
         memset(used,0,sizeof(used));
         memset(len,0,sizeof(len));
         for(int i=0;i<N;i++){
              scanf("%s",str[i]);
              length[i]=strlen(str[i]);
         }
         for(int i=0;i<N;i++){
             for(int j=0;j<N;j++){
                 len[i][j]=getmore(str[i],str[j]);
             }
         }
         for(int i=0;i<N;i++){
             used[i]=1;
             dfs(i,length[i],1);
             used[i]=0;
         }
         printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值