Description
The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently we know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Given several segments of a gene, you are asked to make a shortest sequence from them. The sequence should use all the segments, and you cannot flip any of the segments.
For example, given 'TCGG', 'GCAG', 'CCGC', 'GATC' and 'ATCG', you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one).
For example, given 'TCGG', 'GCAG', 'CCGC', 'GATC' and 'ATCG', you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one).

Input
The first line is an integer T (1 <= T <= 20), which shows the number of the cases. Then T test cases follow. The first line of every test case contains an integer N (1 <= N <= 10), which represents the number of segments. The following N lines express N segments, respectively. Assuming that the length of any segment is between 1 and 20.
Output
For each test case, print a line containing the length of the shortest sequence that can be made from these segments.
Sample Input
1 5 TCGG GCAG CCGC GATC ATCG
Sample Output
11
题意:给定若干基因片段,求包含所有片段的最短基因序列长度。
思路:预处理下被包含的串。这些串不用考虑。
然后预处理每个串接在另外一个串后需要的长度。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define maxn 12 #define inf 0x3f3f3f3f int ans,n; bool vis[maxn]; bool bekill[maxn]; int cost[maxn][maxn]; struct Item { char str[22]; bool operator < (const Item & a)const { if(strlen(str) != strlen(a.str)) return strcmp(str,a.str) > 0; return strlen(str) > strlen(a.str); } }item[maxn]; int Judge(char * s1,char * s2)//s1是否包含s2 { int len1 = strlen(s1),len2 = strlen(s2); for(int i = 0;i <= len1- len2;i++) { bool flag = true; for(int j = 0;j < len2;j++) { if(s1[i+j] != s2[j]) { flag = false; break; } } if(flag) return 1; } return 0; } int query(char * s1,char * s2) { int len1 = strlen(s1),len2 = strlen(s2); for(int i = len1-len2;i < len1;i++) { bool flag = true; for(int j = i;j < len1;j++) { if(s1[j] != s2[j-i]) { flag = false; break; } } if(flag) return len2 - len1 + i; } return len2; } void dfs(int c,int pre) { if(c > ans) return; if(pre == -1) { for(int i = 1;i <= n;i++) if(!bekill[i]) { vis[i] = 1; dfs(strlen(item[i].str),i); vis[i] = 0; } return; } bool flag = false; for(int i = 1;i <= n;i++) { if(!bekill[i] && !vis[i]) { flag = true; vis[i] = 1; dfs(c+cost[pre][i],i); vis[i] = 0; } } if(!flag) ans = min(ans,c); } int main() { //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 1;i <= n;i++) scanf("%s",item[i].str); sort(item+1,item+1+n); memset(bekill,0,sizeof(bekill)); memset(vis,0,sizeof(vis)); for(int i = 2;i <= n;i++) for(int j = 1;j < i;j++) if(Judge(item[j].str,item[i].str)) { bekill[i] = 1; break; } for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) cost[i][j] = query(item[i].str,item[j].str); ans = inf; dfs(0,-1); printf("%d\n",ans); } return 0; }