`

POJ 1699 Best Sequence (DFS)

    博客分类:
  • ACM
 
阅读更多
Best Sequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4156   Accepted: 1645

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

 

题意:现在我们知道基因由脱氧核苷酸组成。组成脱氧核苷酸的碱基有A(adenine), C(cytosine), G(guanine), T(thymine)四种。现在给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因。

 

思路:深度搜索,剪枝。求最短父序列。把数据读入之后,先计算字符串m接在字符串n前面总字符串长度将增加多少,保存在addlen[m][n]中。例如 BCAT 接在 ATCG 前面,则增加的长度为2,然后就用addlen[][]数组进行搜索,使用used[]数组标记本次深度搜索该字符串是否被使过,保证每次深度搜索都能使每个字符串用到且仅使用一次。当深度达到字符串总数n时,表明所有的字符串都接在一起了,比较当前长度与之前最短长度的值,保留最短长度。

 

 

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

using namespace std;

int n,len[20];
char str[20][30];
int add[20][20],vis[20],ans;

int addstr(int m,int n){
    int i,j,k,ans=0;
    for(k=1;k<=len[m] && k<=len[n];k++){
        int flag=1;
        for(i=0,j=len[m]-k;i<len[n] && j<len[m];i++,j++)
            if(str[m][j]!=str[n][i]){
                flag=0;
                break;
            }
        if(flag)
            ans=k;
    }
    return ans;
}

void DFS(int cur,int cnt,int sum){
    if(sum>=ans)
        return ;
    if(cnt==n){
        if(sum<ans)
            ans=sum;
        return ;
    }
    for(int i=0;i<n;i++)
        if(!vis[i] && cur!=i){
            vis[i]=1;
            DFS(i,cnt+1,sum+len[i]-add[cur][i]);
            //printf("----   %d\n",sum+len[i]-add[cur][i]);
            vis[i]=0;
        }
}

int main(){

    //freopen("input.txt","r",stdin);

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",str[i]);
            len[i]=strlen(str[i]);
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(i!=j)
                    add[i][j]=addstr(i,j);

        ans=100000;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++){
            vis[i]=1;
            DFS(i,1,len[i]);
            vis[i]=0;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics