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).
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; }
相关推荐
poj 1699的代码和方法说明,个人原创
poj 1619 EKG Sequence.md
北大POJ1019-Number Sequence 解题报告+AC代码
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
poj 2442 Sequence.md
poj 3623 Best Cow Line, Gold.md
北大POJ3733-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ1020-Anniversary Cake 解题报告+AC代码
北大POJ3373-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ3009-Curling 2.0【DFS+Vector+回溯+剪枝】 解题报告+AC代码
北大POJ3982-The Fibonacci sequence 解题报告+AC代码
北大POJ1014-Dividing【DFS】【多重背包+二进制优化】 解题报告+AC代码
北大POJ1691-Painting A Board 【拓扑+DFS】 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
POJ-2870 Light Up + DFS(1级DFS+1级DFS) + Python - 思维导图 链接:https://blog.csdn.net/xxdragon126/article/details/122095922?spm=1001.2014.3001.5501
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
poj2599,简单dfs可以过此题,按顺序搜到任意可行解即可。
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
poj分类poj分类poj分类poj分类