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;}
- hdu 1583 字符串合并加全排列
- 2013软件大赛 全排列加数字合并
- uva140Bandwidth带宽——字符串处理加全排列
- 字符串的全排列
- 字符串的全排列
- 字符串全排列问题
- 字符串全排列
- 字符串全排列问题
- 字符串全排列
- 求字符串全排列
- 求字符串全排列
- 字符串的全排列
- 字符串全排列代码
- 字符串全排列
- 字符串全排列
- 字符串全排列
- 字符串的全排列
- 字符串全排列
- 羽毛球双打比赛中两位运动员谁来发球以及如何换位?
- leetcode 202 Happy Number
- python string
- 函数可重入性及编写规范
- Spark 定制版:015~Spark Streaming源码解读之No Receivers彻底思考
- hdu 1583 字符串合并加全排列
- Java入门需掌握的30个基本概念
- Java进阶(三)多线程开发关键技术
- ZooKeeper 安装部署及hello world
- python简单之isinstance,type使用
- sublime编辑器【快捷键】基本用法,sublime小技巧
- [leetcode]Single Number
- [DP] BZOJ 4247 挂饰
- [转]两种高性能I/O设计模式(Reactor/Proactor)的比较