HDU-1560 DNA sequence (迭代深搜)
来源:互联网 发布:ajax怎么解析json数据 编辑:程序博客网 时间:2024/06/09 18:53
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.
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.
14ACGTATGCCGTTCAGT
8
嗯,开始直接暴力枚举的各个字符串顺序,有点蠢。。。题意理解有点偏。
利用point指针数组来记录状态。
可以枚举长度,枚举4个字符。
然后利用check的剪枝。
对于当前你已经利用的字符个数n,可以实现匹配的状态是point[i]那么,
对于剩余的字符个数sum和每个字符串剩余的字符数,就是一个上面的子问题,我们可以递归解决。
直到所有字符都可以匹配,这样就找到解了。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int MAXN = 10;const int inf = 1e9;int n,m;//记录字符串char s[MAXN][MAXN];//记录字符串的长度int sl[MAXN];//记录字符串当前匹配到哪了int point[MAXN];char DNA[]="AGCT";int check(){ int res = 0; for(int i = 0; i < n; ++i)res = max(res,sl[i] - point[i]); return res;}int flag;void dfs(int sum){ int c = check(); //如果找到了解 if(!c) { flag = 1; return; } //当前不可能符合条件 if(sum < c)return; //保存现场 int temp[10]; for(int i = 0; i < n; ++i)temp[i] = point[i]; //搜索开始 //表示是否有变化 bool ok = 0; //枚举添加上的字符 for(int i = 0; i < 4; ++i) { //更新point的状态 for(int j = 0; j < n; ++j) { if(s[j][point[j]] == DNA[i]) { point[j]++; ok = 1; } } //如果更新了 if(ok) { //继续枚举下一个字符 dfs(sum-1); if(flag)return; //恢复现场 for(int j = 0 ;j < n; ++j)point[j] = temp[j]; } }}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&n); int ans = 0; for(int i = 0; i < n; ++i) { scanf("%s",s[i]); sl[i] = strlen(s[i]); point[i] = 0; ans = max(ans,sl[i]); } flag = 0; while(1) { dfs(ans); if(flag)break; ans++; } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- HDU-1560-DNA sequence(迭代深搜)
- HDU-1560 DNA sequence (迭代深搜)
- HDU - 1560 DNA sequence
- HDU-1560 DNA sequence
- HDU 1560DNA sequence
- HDU 1560 - DNA sequence
- HDU - 1560----DNA sequence
- HDU - 1560----DNA sequence
- HDU 1568 DNA sequence(迭代深搜)
- hdu 1560 DNA sequence (IDA*)
- HDU-1560 DNA sequence(IDA*)
- hdu 1560 DNA sequence(IDA*)
- HDU 1560 DNA sequence(BFS+hash)
- HDU 1560 DNA sequence(IDA*搜索)
- HDU 1560 DNA sequence(状压+搜索)
- HDU 1560 DNA sequence(IDA*)
- HDU 1560 DNA sequence(IDA*)
- HDU 1560 DNA sequence IDA*
- CSUACM月赛1971
- HDU 6042 Journey with Knapsack(生成函数)
- 最多有多少个点在同一条直线上
- JAVA基础10 Object类和String类
- bzoj 1189 紧急疏散 网络流
- HDU-1560 DNA sequence (迭代深搜)
- Linux修改/etc/profile导致卡在登陆界面
- python_对象整理
- C# TabControl重写
- hdu6053 TrickGCD
- 最高指挥官
- 【每日一题-12】平衡二叉树/镜像与杨氏矩阵查找
- LINUX grep练习题
- poj1729(bfs+优先队列优化)