Best Sequence
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5979 | Accepted: 2329 |
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).
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
题目链接:http://poj.org/problem?id=1699
题意:现在我们知道基因由脱氧核苷酸组成。组成脱氧核苷酸的碱基有A(adenine), C(cytosine), G(guanine), T(thymine)四种。现在给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因。
思路:深度搜索,剪枝。求最短父序列。把数据读入之后,先计算字符串m接在字符串n前面总字符串长度将增加多少,保存在addlen[m][n]中。例如 BCAT 接在 ATCG 前面,则增加的长度为2,然后就用addlen[][]数组进行搜索,使用used[]数组标记本次深度搜索该字符串是否被使过,保证每次深度搜索都能使每个字符串用到且仅使用一次。当深度达到字符串总数n时,表明所有的字符串都接在一起了,比较当前长度与之前最短长度的值,保留最短长度。
时空分析:空间代价是O(n*n),最坏时间代价是O(n^n),运用剪枝,很多情况下就没有搜到第n层。
以上来自大牛的资料:http://blog.sina.com.cn/s/blog_6635898a0100hhtl.html
这个题网上有很多种解法,dp,AC自动机......这是其中一种,其实就是优雅的暴力
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[11][110];
int ans;
int addlen[11][11];
int len[11];
bool used[11];
void add(int m,int n){
int k;
k=0;
for(int l=1;l<=len[m]&&l<=len[n];l++){
bool flag=true;
for(int i=0,j=len[m]-l;i<l;i++,j++){
if(s[m][j]!=s[n][i]){
flag=false;
break;
}
}
if(flag)
k=l;
}
addlen[m][n]=len[n]-k;
}
int n;
void dfs(int pre,int length,int sum){
if(sum>=ans){
return ;
}
if(length==n){
if(sum<ans)
ans=sum;
return ;
}
for(int i=0;i<n;i++){
if(used[i]==false){
used[i]=true;
dfs(i,length+1,sum+addlen[pre][i]);
used[i]=false;
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",s[i]);
len[i]=strlen(s[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
add(i,j);
}
}
ans=1000;
memset(used,false,sizeof(used));
for(int i=0;i<n;i++){
used[i]=true;
dfs(i,1,len[i]);
used[i]=false;
}
cout<<ans<<endl;
}
return 0;
}