zoukankan      html  css  js  c++  java
  • poj1699 KMP+壮压DP

    Best Sequence
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 6338   Accepted: 2461

    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


    /****************************************/
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int INF=0x3f3f3f3f;
    int nxt[22],add[12][12];
    int dp[(1<<12)+5][12];
    char str[12][25];
    void getNext(char *a,int len)
    {
        int i=0,j=-1;
        nxt[0]=-1;
        while(i<len)
        {
            if(j==-1||a[i]==a[j]) nxt[++i]=++j;
            else j=nxt[j];
    
        }
    }
    int kmp(char *a,char *b,int l1,int l2)
    {
        int i=0,j=0;
        while(i<l1)
        {
            if(j==-1||a[i]==b[j]) ++i,++j;
            else j=nxt[j];
        }
        return l2-j;
    }
    int main()
    {
        int n,T;
        for(scanf("%d",&T); T--;)
        {
            scanf("%d",&n);
            for(int i=0; i<n; ++i) scanf("%s",str[i]);
            memset(dp,INF,sizeof(dp));
            for(int i=0; i<n; ++i) dp[(1<<i)][i]=strlen(str[i]);
            for(int i=0; i<n; ++i) for(int j=0; j<n; ++j)
                {
                    getNext(str[j],strlen(str[j]));
                    add[i][j]=kmp(str[i],str[j],strlen(str[i]),strlen(str[j]));
                }
            for(int i=1;i<(1<<n);++i) for(int j=0;j<n;++j) if(dp[i][j]==INF) continue;
            else for(int k=0;k<n;++k) {
                if((1<<k)&i) continue;
                dp[i|(1<<k)][k]=min(dp[(1<<k)|i][k],dp[i][j]+add[j][k]);
            }
            int ans=INF;
            for(int i=0;i<n;++i) ans=min(ans,dp[(1<<n)-1][i]);
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    CentOS6.0/RedHat Server 6.4安装配置过程 详细图解!
    关于Haproxy安装和配置:负载配置【haproxy.cfg】问题记录
    菜鸟学习Struts——bean标签库
    2013——2014总结
    高效程序员的45个习惯读书 ——敏捷开发修炼之道笔记之态度决定一切
    Hive深入浅出
    Java从入门到精通——调错篇之SVN 出现 Loced错误
    考试系统优化——准备工作
    深入解析:分布式系统的事务处理经典问题及模型(转载分享)
    黑客攻击 UVa11825
  • 原文地址:https://www.cnblogs.com/mfys/p/7402231.html
Copyright © 2011-2022 走看看