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\n",mi);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值