• poj1699(状态压缩dp)


    可能没有完全读懂题意。

    个人觉得

    acca

    aa 

    答案应该是4.

    然后就是dp了。。这题数据量小很多方法都可以,数据也水暴力据说都能过。。

    还有就是我竟然没有用扩展kmp优化下。。。 太无耻了,我是因为找扩展kmp的题才来看这题的。

    Best Sequence
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 4217   Accepted: 1668

    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

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <map>
    #include <queue>
    #include <sstream>
    #include <iostream>
    using namespace std;
    #define INF 0x3fffffff
    
    int n;
    char save[11][22];
    char g[11][22];
    int mark[11];
    int dp[11][11][2200];//状态压缩
    int top=0;
    int sum[11][11];
    
    
    int check(char s[],char t[])
    {
        int len=strlen(s);
        int len1=strlen(t);
        int cnt=0;
        int flag=0;
        for(int i=0;i<len1;i++)
        {
            int tmp=i;
            while(tmp<len1&&s[cnt]==t[tmp])
            {
                cnt++;
                tmp++;
                if(cnt==len)
                {
                    flag=1;
                    break;
                }
            }
            cnt=0;
        }
        return flag;
    }
    
    int main()
    {
        //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
        //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(mark,0,sizeof(mark));
            scanf("%d",&n);
            for(int i=0;i<n;i++)
                scanf("%s",save[i]);
            //////////////
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if(i==j) continue;
                    if(mark[j]==0&&check(save[i],save[j])==1)
                    {
                        mark[i]=1;
                        break;
                    }
                }
            }
            top=0;
    
            for(int i=0;i<n;i++)
            {
                if(mark[i]==0)
                {
                    strcpy(g[top],save[i]);
                    top++;
                }
            }
    
            //////////////////////////////先做个预处理比较方便吧
            memset(sum,0,sizeof(sum));
    
            for(int i=0;i<top;i++)
                for(int j=0;j<top;j++)
                {
                    if(i==j) continue;
                    int len=strlen(g[i]);
                    int len1=strlen(g[j]);
                    for(int k=min(len,len1)-1;k>=0;k--)
                    {
                        int flag=0;
                        for(int i1=0;i1<k;i1++)
                            if(g[i][len-k+i1]!=g[j][i1])
                            {
                                flag=1;
                                break;
                            }
                        if(flag==0)
                        {
                            sum[i][j]=len1-k;
                            break;
                        }
                        else sum[i][j]=len1;
                    }
                }
    
            //这样就求出sum了
            ////////////////然后剩下的就是不会互相影响的了
    
            for(int ii=0;ii<=10;ii++)
                for(int i=0;i<=10;i++)
                    for(int j=0;j<1100;j++)
                        dp[ii][i][j]=INF;
    
            for(int i=0;i<top;i++)
            {
                int len=strlen(g[i]);
                dp[0][i][ (1<<i) ] = len;
            }
    
            for(int ii=1;ii<top;ii++)
            {
                for(int j=0;j<top;j++)
                {
                    for(int k=0;k<(1<<top);k++)
                    {
                        if(dp[ii-1][j][k]==INF) continue;
                        for(int i=0;i<top;i++)
                        {
                            if( ( k&(1<<i) )==0 )
                            {
                                //在之前没有出现过的时候..
                                dp[ii][i][(k|(1<<i))]=min(dp[ii][i][( k|(1<<i) )],dp[ii-1][j][k]+sum[j][i]);
                            }
                        }
                    }
                }
            }
    
            int mi=INF;
            for(int i=0;i<top;i++)
                for(int j=0;j<(1<<top);j++)
                    mi=min(dp[top-1][i][j],mi);
            printf("%d
    ",mi);
        }
        return 0;
    }
  • 相关阅读:
    import和include的区别
    $sformat用法
    如何快速理解DUT
    vim_basic
    UVM——寄存器模型相关的一些函数
    AMBA——总线仲裁
    Cache的写回策略(转)
    Cache直接映射、组相连映射以及全相连映射(转载)
    一起学IC验证:推荐资料合集,收藏专用(转载)
    VCS仿真流程
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3225338.html
Copyright © 2020-2023  润新知