HDU1560 DNA sequence(IDA*)题解

DNA sequence

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3503    Accepted Submission(s): 1681


Problem Description
The twenty-first century is a biology-technology developing century. 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). Finding the longest common subsequence between DNA/Protein sequences is one of the basic problems in modern computational molecular biology. But this problem is a little different. Given several DNA sequences, you are asked to make a shortest sequence from them so that each of the given sequence is the subsequence of it.

For example, given "ACGT","ATGC","CGTT" and "CAGT", you can make a sequence in the following way. It is the shortest but may be not the only one.

 

Input
The first line is the test case number t. Then t test cases follow. In each case, the first line is an integer n ( 1<=n<=8 ) represents number of the DNA sequences. The following k lines contain the k sequences, one per line. Assuming that the length of any sequence is between 1 and 5.
 

Output
For each test case, print a line containing the length of the shortest sequence that can be made from these sequences.
 

Sample Input
 
  
14ACGTATGCCGTTCAGT
 
Sample Output
 
  
8

思路:

刚开始BFS就爆内存了。

新思路是给dfs加一个最小限制,超过限制就返回,然后不断加大限制直到符合,那么此时dfs的答案也是最小的。这里有几个要剪枝的地方:一是超过限制剪枝;二是预估值+当前值超过限制也要剪枝。

一开始看的那些题解都看不懂的我emmmm......orz

借鉴题解:链接


Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
//#include<map>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=810;
using namespace std;
char str[10][10],dna[4]={'A','C','G','T'};
int n,deep,ans,len[10];
void dfs(int step,int pos[]){	//step为当前长度 
	if(step>deep) return;	//超过深度返回 
	int maxdeep=0;
	for(int i=0;i<n;i++){
		maxdeep=max(len[i]-pos[i],maxdeep);	//maxdeep为预估剩余深度  
	} 
	if(step+maxdeep>deep) return;	//当前长度加预估剩余深度大于deep,剪枝 
	if(maxdeep==0){	//所有串都满足
		ans=deep;
		return;
	}
	int temp[10],flag;
	for(int i=0;i<4;i++){
		flag=0;
		for(int j=0;j<n;j++){
			if(str[j][pos[j]]==dna[i]){
				temp[j]=pos[j]+1;
				flag=1;
			}
			else temp[j]=pos[j];
		}
		if(flag){
				dfs(step+1,temp);
		}
		if(ans) return;
	}
} 
 int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		deep=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%s",str[i]);
			len[i]=strlen(str[i]);
			deep=max(deep,len[i]);	//找出最长的作为第一次深搜最小深度 
		}
		ans=0;
		int pos[10];	//表示第i组验证到第pos[i]个 
		memset(pos,0,sizeof(pos));
		while(1){
			dfs(0,pos);
			if(ans) break;
			deep++;	//加深迭代深度,重新DFS 
		}
		printf("%d\n",ans);
	}
	return 0;
}


转载于:https://www.cnblogs.com/KirinSB/p/9409128.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值