hdu 1583 字符串合并加全排列

来源:互联网 发布:枫树浏览器 知乎 编辑:程序博客网 时间:2024/05/19 06:49

DNA Assembly
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 227 Accepted Submission(s): 96

Problem Description
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.

Input
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.

Output
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.

Sample Input
4
GATTA
TAGG
ATCGA
CGCAT

Sample Output
13

HintHint
Explanation of the sample:

Such string is “CGCATCGATTAGG”.

#include<iostream>#include<cstdio>#include<algorithm>#include<string>#include<cmath>#define INF 0xffffffusing namespace std;string str_merge(string a,string b){    int lena=a.length();    int lenb=b.length();    int flag=0;    int pos;    if(a=="")return b;    for(int i=1;i<=lena;i++){        if(a.substr(lena-i,i)==b.substr(0,i)){            flag=1;            pos=i;        }    }    if(flag){        return a+b.substr(pos,lenb-pos);    }    else {        return a+b;    }}int main(){    string s[7];    int a[7];    int n;    int ans;    while(cin>>n){        for(int i=0;i<n;i++){            cin>>s[i];            a[i]=i;        }        ans=INF;        do{            string tt="";            for(int i=0;i<n;i++){                tt=str_merge(tt,s[a[i]]);            }            if(tt.length()<ans){                ans=tt.length();            }        }while(next_permutation(a,a+n));        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击