zoukankan      html  css  js  c++  java
  • POJ 1699 kmp+枚举

    Best Sequence
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5135   Accepted: 2040

    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).

    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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 const int inf=0x3fffffff;
     9 const int maxn=11;
    10 char str[maxn][21];
    11 int cost[maxn][maxn];
    12 int len[maxn];
    13 int n,t;
    14 
    15 int cal(char *st1, char *st2)
    16 {
    17     int len1 = strlen(st1);
    18     int len2 = strlen(st2);
    19     for (int i = len1 - len2; i < len1; i++)
    20     {
    21         bool ok = true;
    22         for (int j = i; j < len1; j++)
    23             if (st1[j] != st2[j - i])
    24             {
    25                 ok = false;
    26                 break;
    27             }
    28         if (ok)
    29             return len2 + i - len1;
    30     }
    31     return len2;
    32 }
    33 void work()
    34 {
    35     for(int i=0;i<n;i++)
    36         for(int j=0;j<n;j++)
    37     {
    38         cost[i][j]=cal(str[i],str[j]);
    39     }
    40 }
    41 int Get_ans()
    42 {
    43     int next[maxn];int ans=inf,sum;
    44     for(int i=0;i<n;i++) next[i]=i;
    45     do{
    46         sum=len[next[0]];
    47         for(int i=0;i<n-1;i++)
    48         {
    49             sum+=cost[next[i]][next[i+1]];
    50         }
    51          ans=min(ans,sum);
    52         }while(next_permutation(next,next+n));
    53         return ans;
    54 }
    55 int main()
    56 {
    57     //freopen("in.txt","r",stdin);
    58     scanf("%d",&t);
    59     while(t--){
    60             memset(str,0,sizeof(str));
    61             scanf("%d",&n);
    62             for(int i=0;i<n;i++)
    63             {
    64                 scanf("%s",str[i]);
    65                 len[i]=strlen(str[i]);
    66             }
    67             work();
    68             int ans=Get_ans();
    69             printf("%d
    ",ans);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    模板
    Codeforces Round #598 (Div. 3)
    Codeforces Round #589 (Div. 2)
    日记集
    模板
    Codeforces Round #592 (Div. 2)
    Linux挂载新盘
    VMware实用技巧
    useradd和adduser的区别
    ubuntu VSFTPD搭建FTP服务器 提示530错误
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4282312.html
Copyright © 2011-2022 走看看