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

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