POJ 1699 Best Sequence
2015-09-07 22:36
423 查看
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).InputThe 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.OutputFor 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 ATCGSample Output
11
解题思路:转化成经典TSP问题进行求解,总的长度减去路径最长的长度即为答案。
#include <ctime>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <map>#include <set>#include <numeric>#include <algorithm>#include <functional>using namespace std;const int maxn = 30;const int inf = 0x3f3f3f3f;int g[maxn][maxn];string str[20];int T, n;int dp[(1<<10)+10][15];void init() { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(i == j) { g[i][j] = 0; } else { g[i][j] = 0; int l1 = str[i].length(); int l2 = str[j].length(); for(int k = l1 - 1; k >= 0 && l1 - k <= l2; --k) { if(str[i].substr(k, l1 - k) == str[j].substr(0, l1 - k)) { g[i][j] = l1 - k; } } } } }}int main() { //freopen("aa.in", "r", stdin); int ans; scanf("%d", &T); while(T--) { scanf("%d", &n); ans = 0; for(int i = 0; i < n; ++i) { cin >> str[i]; ans += str[i].length(); } init(); memset(dp, -inf, sizeof(dp)); for(int i = 0; i < n; ++i) { dp[1<<i][i] = 0; } for(int i = 1; i < (1<<n); ++i) { for(int j = 0; j < n; ++j) { if(dp[i][j] >= 0 || !(i&(1<<j))) continue; for(int k = 0; k < n; ++k) { if((i&(1<<k)) && k != j) { dp[i][j] = max(dp[i][j], dp[i^(1<<j)][k] + g[k][j]); } } } } int maxc = 0; for(int i = 0; i < n; ++i) { maxc = max(maxc, dp[(1<<n)-1][i]); } printf("%d\n", ans - maxc); } return 0;}
相关文章推荐
- CCMenuItem解析
- UIScrollView和UIPageControl结合
- UIScrollView的缩放
- UI 设计 绘图(涂鸦)
- 内核工作队列workqueue
- 搭建go开发环境时,出现GoSublime error: MarGo build failed的问题
- POJ3368--Frequent values
- ContentValues、HashTable、HashMap异同
- 黑马程序员-OC基础之NSValue的使用
- EasyUI -datagrid分页后台查询
- ByBueredStream
- [APUE] 再读之进程关系
- 在现实编程中使用fluent风格
- POJ 1947 Rebuilding Roads
- 《APUE》读书笔记—进程控制
- 禁止Ubuntu开机进入guest session
- unique函数
- LeetCode(60) Permutation Sequence
- LeetCode(60) Permutation Sequence
- Codeforces 156D Clues (图论, 并查集, 卡特兰数)