Farmer John has performed DNA sequencing on his prize milk-producing cow, Bessie DNA sequences are ordered lists (strings) containing the letters 'A', 'C', 'G', and 'T'.
As is usual for DNA sequencing, the results are a set of strings that are sequenced fragments of DNA, not entire DNA strings. A pair of strings like 'GATTA' and 'TACA' most probably represent the string 'GATTACA' as the overlapping characters are merged, since they were probably duplicated in the sequencing process.
Merging a pair of strings requires finding the greatest overlap between the two and then eliminating it as the two strings are concatenated together. Overlaps are between the end of one string and beginning of another string, NOT IN THE MIDDLE OF A STRING.
By way of example, the strings 'GATTACA' and 'TTACA' overlap completely. On the other hand, the strings 'GATTACA' and 'TTA' have no overlap at all, since the matching characters of one appear in the middle of the other, not at one end or the other. Here are some examples of merging strings, including those with no overlap:
GATTA + TACA -> GATTACA
TACA + GATTA -> TACAGATTA
TACA + ACA -> TACA
TAC + TACA -> TACA
ATAC + TACA -> ATACA
TACA + ACAT -> TACAT
Given a set of N (2 <= N <= 7) DNA sequences all of whose lengths are in the range 1..7, find and print length of the shortest possible sequence obtainable by repeatedly merging all N strings using the procedure described above. All strings must be merged into the resulting sequence.
The input consists of multiple test cases.
Each test case :
Line 1: A single integer N
Lines 2..N+1: Each line contains a single DNA subsequence
End of file.
For each pair of input output the length of the shortest possible string obtained by merging the subsequences. It is always possible – and required – to merge all the input strings to obtain this string.
13 Hint Hint Explanation of the sample: Such string is "CGCATCGATTAGG".
/*题目意思:让你拼接字符串,使长度最少; 思路:法一:由于N好小,直接暴力模拟,对N个字符串进行全排列,若可以,则算出结果长度;法二:图论:第i个字符串作为点i,枚举i到其他点是否可以拼接,若可以,则有向边,权值为公共长度标记入度,和邻接表vector对入度为0的作为起点枚举,回朔标记点是否访问过,累加权值最大sum所有字符串的长度-最大公共长度=结果 *//*用的法1 不知为何wa? 知道的求告知*/#include #include #include using namespace std;string s[10];int a[10];int map[10][10]; int n,sum,re;inline int check(int x,int y){ int i,j; for(i=0;i >n){ sum=0; //字符串总长 re=-99999999; //最长公共部分 for(int i=0;i >s[i]; sum+=s[i].length(); a[i]=i; } for(int i=0;i