CSU 1611: Concatenation(状态压缩DP)

1611: Concatenation

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 14  Solved: 4

[Submit][Status][Web
Board
]

Description

Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.

Zuosige writes some of his favorite strings on paper. And he wants to find out a string which has all these favorite strings as its substrings. However, as a sick man, he cannot remember a very long string, so he wants you to help him find out the shortest
one.

Input

The first line contains one integer T, indicating the number of test cases.

In one test case, there are several lines.

In the first line, there are two integers n (1<=n<=12), indicating the number of strings he writes.

In the following n lines, each line has a string whose length is no more than 50. The strings only consists of uppercase letters.

Output

For each test case, output an integer indicating the length of the shortest string containing all these strings.

Sample Input

1
2
ABCD
BCDABC

Sample Output

7

HINT

Source

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#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 long
const double pi = acos(-1.0);
#define N 1000005
#define mod 19999997
#define INF 0x3f3f3f3f
#define exp 1e-8

int t,n;
char s[15][55],s1[15][55];
int dp[1<<13][55],len[15],cnt,flag;
int hsh[15][15];
int main()
{
    int i,j,k,x,y;
    cin>>t;
    w(t--)
    {
        cin>>n;
        up(i,0,n-1)
        {
            scanf("%s",s[i]);
        }
        cnt = 0;
        up(i,0,n-1)
        {
            flag = 0;
            up(j,0,n-1)
            {
                if(strstr(s[j],s[i]) && strcmp(s[i],s[j])) flag = 1;
            }
            if(!flag)
                strcpy(s1[cnt++],s[i]);
        }
        n = 0;
        up(i,0,cnt-1)
        {
            flag = 0;
            up(j,0,i-1)
            {
                if(!strcmp(s1[i],s1[j])) flag = 1;
            }
            if(!flag)
                strcpy(s[n++],s1[i]);
        }
        up(i,0,n-1)
        {
            len[i]=strlen(s[i]);
        }
        mem(hsh,0);
        up(i,0,n-1)
        {
            up(j,0,n-1)
            {
                hsh[i][j] = len[j];
                int pos = -1;
                down(k,len[i]-1,1)
                {
                    flag = 1;
                    up(x,k,len[i]-1)
                    {
                        if(s[i][x]!=s[j][x-k]) flag = 0;
                    }
                    if(flag) pos = k;
                }
                if(pos!=-1) hsh[i][j] = len[j]-len[i]+pos;
            }
        }
        mem(dp,INF);
        int size = (1<<n)-1,pre=0,now;
        up(i,1,size)
        {
            up(j,0,n-1)
            {
                if(i&(1<<j))
                {
                    pre = (i^(1<<j));
                    if(pre==0) dp[i][j] = len[j];
                    up(k,0,n-1)
                    {
                        if(j!=k && (i&(1<<k))==0)
                        {
                            now=(i|(1<<k));
                            dp[now][k]=min(dp[now][k],dp[i][j]+hsh[j][k]);
                        }
                    }
                }
            }
        }
        int minn = INF;
        up(i,0,n-1)
        minn = min(minn,dp[size][i]);
        printf("%d\n",minn);
    }

    return 0;
}

/**************************************************************
    Problem: 1611
    User: aking2015
    Language: C++
    Result: Accepted
    Time:20 ms
    Memory:3248 kb
****************************************************************/
时间: 2024-08-12 12:01:50

CSU 1611: Concatenation(状态压缩DP)的相关文章

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

HDU1565(状态压缩dp)

方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8170    Accepted Submission(s): 3095 Problem Description 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数

HDU 3001【状态压缩DP】

题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数据的预处理,利用数组分解各个位数,从而达到类似二进制的目的. 然后就是状态的表示,dp[s][i]表示状态s时到达i的最优值. 状态转移也一目了然,不废话. #include<stdio.h> #include<string.h> #include<algorithm> u

Victor and World(spfa+状态压缩dp)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 958    Accepted Submission(s): 431 Problem Description After trying hard fo

poj 3311 Hie with the Pie(状态压缩dp)

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be

HDU--1074(状态压缩DP)

典型的状态压缩DP,给出了每件作业的截止时间和花费,求让老师扣分最少的写作业方式.把完成n种作业用状态2^n-1表示,dp[s]表示 完成状态s时,最小扣分.比如“111”,那么可以由“011”,“110”,“101”转移过来,分别表示选了0,1号作业,1,2号作业,0,2号作业. t[s]表示状态S记录的总时间.dp[s] = min{dp[j]+c[k] - d[k]},其中j = i^(1<<k),0<k<n;pre[s]表示状态s完成时,最末尾完成的作业, #include

poj 3254 Corn Fields(状态压缩dp)

Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and

poj 2411 Mondriaan&#39;s Dream(状态压缩+dp)

 题意:用1*2砖块铺满n*m的房间. 思路转自:http://www.cnblogs.com/scau20110726/archive/2013/03/14/2960448.html 因为这道题输入范围在11*11之间,所以可以先打表直接输出.......... 状态压缩DP 经典覆盖问题,输入n和m表示一个n*m的矩形,用1*2的方块进行覆盖,不能重叠,不能越出矩形边界,问完全覆盖完整个矩形有多少种不同的方案 其中n和m均为奇数的话,矩形面积就是奇数,可知是不可能完全覆盖的.接着我们来看

1252 - Twenty Questions(状态压缩DP)

经典的状态压缩DP .  有没有感觉这道题和什么东西有点像?  没错,是01背包 . 将特征看作物品 , 只不过这里的状态有点复杂, 需要用一个集合才能表示它, 所以我们用d[s][a]来表示,已经询问了特征集s , 假设我们要猜的物品是w ,w所具备的特征集为a ,此时还要询问的最小次数 .   显然a是s的子集,而且要注意本题的要求, 求的是最小化的最大询问次数 .也就是说无论猜哪个物品,猜这么多次一定能猜到 . 那么状态如何转移呢? 就像背包问题,对于一个特征k ,我们要抉择:要k还是不要