HDU 1583 DNA Assembly(暴力模拟)

DNA Assembly

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 213 Accepted Submission(s): 88


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
Hint
Hint
Explanation of the sample: Such string is "CGCATCGATTAGG".

Source

/*
题目意思:让你拼接字符串,使长度最少; 
思路:
法一:由于N好小,直接暴力模拟,对N个字符串进行全排列,若可以,则算出结果长度;
法二:图论:
第i个字符串作为点i,枚举i到其他点是否可以拼接,若可以,则有向边,权值为公共长度
标记入度,和邻接表vector
对入度为0的作为起点枚举,回朔标记点是否访问过,累加权值最大sum
所有字符串的长度-最大公共长度=结果 
*/
/*用的法1  不知为何wa?  知道的求告知*/
#include <iostream>
#include <string>
#include <algorithm>
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<s[x].length();i++){
     	for(j=0;j<s[y].length()&&i+j<s[x].length();j++){
     		 if(s[x][i+j]!=s[y][j])
     		    break;
		 }	
		 	if(i+j==s[x].length()) //前者必须到结尾完 
		 	  return j;
	 }
	 return -1;
}
int main()
{  
    while(cin>>n){
    	sum=0;  //字符串总长 
    	re=-99999999;   //最长公共部分 
    	for(int i=0;i<n;i++){
    		cin>>s[i];
    		sum+=s[i].length();
    		a[i]=i;
		}
		for(int i=0;i<n;i++){   //一点到其他点的公共长度; 
			for(int j=0;j<n;j++){
				if(j==i){
					map[i][j]=-1;
					continue;
				}				 
				map[i][j]=check(i,j);			
			}		
		}		
		/*	for(int i=0;i<n;i++){   
			for(int j=0;j<n;j++)
				cout<<map[i][j]<<" ";
			cout<<endl;*/		
		do{
		    bool ok=1;
		    int tmp=0;
		    for(int i=0;i<n-1;i++){
		    	if(map[a[i]][a[i+1]]==-1)
		    	  {
		    	  	 ok=0;
		    	  	 break;
				  }	
				 else{
				 	tmp+=map[a[i]][a[i+1]];
				 }	 
			}
			if(ok){
				if(re<tmp)
				  re=tmp;
			}
		}while(next_permutation(a,a+n));
		cout<<sum-re<<endl; 
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值