Description
Input
Output
Sample Input
1 5 TCGG GCAG CCGC GATC ATCG
Sample Output
11
Source
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #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 Len 100005 #define mod 19999997 const int INF = 0x3f3f3f3f; char str[15][25]; int t,n,dp[15][15],len[15],vis[15]; int cal(int x,int y) { int i,j,sum,flag; up(i,0,len[x]-1) { int ly = 0; sum = 0; flag = 0; up(j,i,len[x]-1) { if(str[x][j]!=str[y][ly]) { flag = 1; break; } ly++; sum++; } if(!flag) return len[x]-sum; } return len[x]; } int dfs(int now,int step) { int ans = INF,i; if(step == n) return len[now]; up(i,0,n-1) { if(!vis[i]) { vis[i] = 1; int tmp = dp[now][i]; tmp+=dfs(i,step+1); vis[i] = 0; ans = min(ans,tmp); } } return ans; } int main() { int i,j; scanf("%d",&t); w(t--) { mem(dp,0); scanf("%d",&n); up(i,0,n-1) { scanf("%s",str[i]); len[i] = strlen(str[i]); } up(i,0,n-1) { up(j,0,n-1) { if(i==j) continue; dp[i][j] = cal(i,j); } } mem(vis,0); int ans = INF; up(i,0,n-1) { vis[i] = 1; ans = min(ans,dfs(i,1)); vis[i] = 0; } printf("%d\n",ans); } return 0; }
原文地址:http://blog.csdn.net/libin56842/article/details/45174575