Poj1699:best Sequence (DP)

Source: Internet
Author: User

Description

The Twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks are on the gene project, especially in the gene sorting program. Recently we know a gene is made of DNA. The nucleotide bases from which DNA was built is A (adenine), C (cytosine), G (guanine), and T (thymine). Given several segments of a gene, you is 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 and get a SE Quence of length 11. It is the shortest sequence (and may be isn't the only one).

Input

The first line was an integer t (1 <= t <=), 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 <=), which represents the number of segments. The following n lines express n segments, respectively. Assuming 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 is made from these segments.

Sample Input

15TCGGGCAGCCGCGATCATCG

Sample Output

11

Source

POJ monthly--2004.07.18

And HDU1560 is the same problem, the problem I wrote with ida*, if the code is directly submitted to the TLE, then it can only be done in a different way, we can use DP to optimize, DP[I][J], the record I string and the J string merge, remove the coincident part, I still how many, So when we search, we just need to enumerate the combinations of DP arrays.
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define Len 100005#define mod 19999997const int INF = 0x3f3f3f3f;char Str[15][25];int t,n,dp[15][15],len[15],vis[15];int C    Al (int x,int y) {int i,j,sum,flag;        Up (i,0,len[x]-1) {int ly = 0;        sum = 0;        Flag = 0;                Up (j,i,len[x]-1) {if (str[x][j]!=str[y][ly]) {flag = 1;            Break            } ly++;        sum++;    } if (!flag) return len[x]-sum; } return len[x];} int dfs (int now,int step) {int ans =Inf,i;    if (step = = N) return Len[now];            Up (i,0,n-1) {if (!vis[i]) {vis[i] = 1;            int tmp = Dp[now][i];            Tmp+=dfs (i,step+1);            Vis[i] = 0;        ans = min (ans,tmp); }} return ans;    int main () {int i,j;    scanf ("%d", &t);        W (t--) {mem (dp,0);        scanf ("%d", &n);            Up (i,0,n-1) {scanf ("%s", Str[i]);        Len[i] = strlen (Str[i]);                } up (i,0,n-1) {up (j,0,n-1) {if (i==j) continue;            DP[I][J] = cal (I,J);        }} mem (vis,0);        int ans = INF;            Up (i,0,n-1) {vis[i] = 1;            ans = min (Ans,dfs (i,1));        Vis[i] = 0;    } printf ("%d\n", ans); } return 0;}


Poj1699:best Sequence (DP)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.