您的位置:首页 > 产品设计 > UI/UE

[poj 1699] Best Sequence dfs+剪支

2016-04-06 19:08 337 查看
Best Sequence

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 5615 Accepted: 2213

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

POJ Monthly–2004.07.18

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

题意

题意:现在给出几个基因片段,要求你将它们排列成一个最短的序列,不能翻转基因。

思路

n方预处理,n!的dfs+ans》now的剪支;

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

char s[12][105];
int n,ans;
int T;
int a[12][12];
int used[12];
int len[12];
int cal(int x,int y)
{
int ll=min(len[x],len[y]);
for(int l=ll;l>=1;l--)
{
int flag=1;
for(int i=0,j=len[x]-l;i<l;i++,j++)
{
if(s[x][i]!=s[y][j])
{
flag=0;
break;
}
}
if(flag) return l;
}
return 0;

}
void dfs(int id,int dep,int sum)
{
//cout<<dep<<"   "<<sum<<endl;
if(sum>=ans) return ;
if(dep==n)
{
ans=min(sum,ans);
return ;
}
for(int i=1;i<=n;i++)
if(!used[i])
{
used[i]=1;
dfs(i,dep+1,sum+a[id][i]);
used[i]=0;
}
}
int main()
{
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]);
len[i]=strlen(s[i]);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
a[i][j]=len[j]-cal(i,j);
ans=1000000;
memset(used,0,sizeof(used));
for(int i=1;i<=n;i++)
{
used[i]=1;
dfs(i,1,len[i]);
used[i]=0;
}
printf("%d\n",ans);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: