1611:concatenation time limit: 1 Sec Memory Limit: MB
Submit: Solved: 4
[Submit] [Status] [Web Board] Description
Zuosige always have bad luck. Recently, he is in hospital because of pneumonia. While he was taking his injection, he feels extremely bored. However, clever zuosige comes up with a new game.
Zuosige writes some of he favorite strings on paper. And he wants to the find out a string which have all these favorite strings as its substrings. However, as a sick man, he cannot remember a very long string, and so he wants the find out the him one.
Input
The first line contains one integer T, indicating the number of test cases.
In one test case, there is several lines.
In the first line, there is integers n (1<=n<=12), indicating the number of strings he writes.
In the following n lines, each of the line have a string whose length is no more than 50. The strings only consists of uppercase letters.
Output
For each test case, output a 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++ Re sult:accepted time:20 Ms memory:3248 kb****************************************************************/
CSU 1611:concatenation (State compression DP)