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
****************************************************************/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值