CSU 1611: Concatenation(状态压缩DP)
来源:互联网 发布:js将list转换为数组 编辑:程序博客网 时间:2024/06/09 17:39
1611: Concatenation
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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
12ABCDBCDABC
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 longconst 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****************************************************************/
- CSU 1611: Concatenation(状态压缩DP)
- CSU 1116: Kingdoms(状态压缩+DP)
- CSU 1964 Problem_hc(状态压缩dp)
- CSU 1611: Concatenation
- CSU1611: Concatenation(状态压缩)
- 文章标题 CSU 1843: Jumping monkey (状态压缩+dp)
- 状态压缩dp
- pku1038状态压缩dp
- 状态压缩DP 入门
- HDU1074 状态压缩DP
- Poj3254 状态压缩DP
- 状态压缩DP入门
- 【状态压缩DP】互不侵犯
- 【状态压缩DP】电子竞技
- HDU1074 状态压缩DP
- 【状态压缩DP】互不侵犯
- 状态压缩 DP AHU420
- HDU2167 状态压缩DP
- day12/WaitNotifyAllProducerConsumer1.java
- 3.linux复习----fork下文件描述符共享
- day12/WaitNotifyAllProducerConsumer.java
- day12/WaitNotifyThread1.java
- 动态规划 编辑距离
- CSU 1611: Concatenation(状态压缩DP)
- hud1326 排序水题
- php中empty()和isset()函数的区别
- day12/WaitNotifyThread.java
- 数据结构:二叉搜索树
- 链表 Remove Duplicates from Sorted List II
- HDU 4586 Play the Dice(规律+推导)
- day13/Jibenshujuleixing.java
- 双向循环链表 函数的实现