Best Sequence----dfs+剪枝
来源:互联网 发布:js面向对象编程的好处 编辑:程序博客网 时间:2024/05/13 00:17
Best Sequence
Time Limit: 1000MS Memory Limit: 10000KTotal 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
15TCGGGCAGCCGCGATCATCG
Sample Output
11
Source
POJ Monthly--2004.07.18
题目链接: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;}
0 0
- Best Sequence----dfs+剪枝
- poj1699--Best Sequence(dfs+剪枝)
- poj 1699—Best Sequence(dfs剪枝)
- 【POJ1699】-Best Sequence 搜索剪枝
- POJ1699 Best Sequence DP DFS
- poj 1699 Best Sequence(dfs)
- Best Sequence poj1699(dfs)
- hdu 2610 Sequence one ( dfs+可行性剪枝 )
- hdu 2610 Sequence one【搜索 dfs+剪枝】
- HDU 2610 Sequence one(dfs+剪枝)
- poj-1699-Best Sequence-dfs+子状态
- POJ 1699 Best Sequence (DFS+预处理)
- POJ--1699--Best Sequence【扩展KMP+DFS】
- POJ--1699 Best Sequence(DP+dfs)
- [poj 1699] Best Sequence dfs+剪支
- HDU 2611 Sequence two (dfs + 重判 + 剪枝)
- poj 1699 Best Sequence(dfs)
- poj 1699 Best Sequence(dfs+A(n,n))
- DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;,?,?,?,?,?,?,?,?);END-OF-STATEMENT
- javaScript内置函数大全
- 学习NodeJs一讲——模块
- 蓝桥杯算法训练 P1103 结构体指针or 暴力
- Android音频开发之尝试音频混合
- Best Sequence----dfs+剪枝
- UBUNTU15.10+NS2.35构建实验环境
- 对XX网站的数据采集
- hashCode
- C++变量存储类型与标识符生命期
- poj1207
- 库与设计模式
- Android 笔试/面试,常见问题整理
- (29)Java学习笔记——常用对象 / System 类