HDU 1560 DNA sequence (迭代加深搜索)
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.
InputThe 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.OutputFor each test case, print a line containing the length of the shortest sequence that can be made from these sequences.Sample Input
1
4
ACGT
ATGC
CGTT
CAGT
Sample Output
8 思路
如果DNA最长的串长度为n,那就先搜索以n为长度,是否存在符合条件的母串,若不存在,再搜索n+1;
这便是所谓的迭代加深搜索。结束。
代码中用到的maxx,只是一个剪枝而已。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = 1e9+;
const double eps = 1e-; char s[][];
int maxs=;
int tot[];
int n,pos[];
char a[]="ACGT"; void view()
{
for(int i=;i<=n;i++){
cout<<pos[i]<<" ";
}
cout<<endl;
} bool dfs(int t)
{ int maxx=;
for(int i=;i<=n;i++){
maxx=max(maxx,tot[i]-pos[i]);
}
if(maxx==){return true;}
if(t+maxx>maxs){return false;}
bool vis[];
for(int i=;i<;i++){
memset(vis,,sizeof(vis));
for(int j=;j<=n;j++){
if(s[j][pos[j]]==a[i]){
pos[j]++;
vis[j]=true;
}
}
if(dfs(t+)){return true;}
for(int j=;j<=n;j++){
if(vis[j]){
pos[j]--;
}
}
}
return false;
} int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
maxs=;
for(int i=;i<=n;i++){
scanf("%s",s[i]);
tot[i]=strlen(s[i]);
maxs=max(maxs,tot[i]);
} while(true){
memset(pos,,sizeof(pos));
if(dfs()){
printf("%d\n",maxs);
break;
}
else maxs++;
}
}
return ;
}
HDU 1560 DNA sequence (迭代加深搜索)的更多相关文章
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 1560 DNA sequence(DNA序列)
HDU 1560 DNA sequence(DNA序列) Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- hdu 1560 DNA sequence(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others) ...
- HDU 1560 DNA sequence(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 题目大意:给出n个字符串,让你找一个字符串使得这n个字符串都是它的子串,求最小长度. 解题思路: ...
- HDU 1560 DNA sequence DFS
题意:找到一个最短的串,使得所有给出的串是它的子序列,输出最短的串的长度,然后发现这个串最长是40 分析:从所给串的最长长度开始枚举,然后对于每个长度,暴力深搜,枚举当前位是哪一个字母,注意剪枝 注: ...
- HDU - 1560 DNA sequence
给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty ...
- HDU 1560 DNA sequence A* 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=1560 仔细读题(!),则可发现这道题要求的是一个最短的字符串,该字符串的不连续子序列中包含题目所给的所有字符串 ...
- HDU1560(迭代加深搜索)
DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- 关于 flask 实现数据库迁移以后 如何根据创建的模型类添加新的表?
在此之前 我们先说一下常规的flask运用第三方扩展来实现数据库的迁移的三个步骤以及每步的目的. 数据库的迁移的三个步骤:(cd 到run.py所在路径) python run.py db init ...
- 学习android开发之路(一)页面布局
Android页面布局 1.Android页面布局一共分为6种: LinearLayout(线性布局).RelativeLayout(相对布局).TableLayout(表格布局).FrameLayo ...
- Luogu1137 旅行计划(拓扑排序)
题目传送门 拓扑排序板子题,模拟即可. 代码 #include<cstdio> #include<iostream> #include<cmath> #includ ...
- [离散时间信号处理学习笔记] 9. z变换性质
z变换描述 $x[n] \stackrel{\mathcal{Z}}{\longleftrightarrow}X(z) ,\quad ROC=R_x$ 序列$x[n]$经过z变换后得到复变函数$X(z ...
- Tyche 2147 旅行
题目描述 你有m元钱,将要游览n个国家.每一个国家有一种商品,其中第i个国家商品的单价为ai元.每到一个国家,你会用手上的钱疯狂购买这个国家的商品,直到剩余的钱无法购买为止. 现在你要决定游览这n个国 ...
- Volatile的应用
.java 的执行过程 Java代码在编译后会变成Java字节码 字节码被类加载器加载到JVM里 JVM执行字节码,转化为汇编指令在CPU上执行 Java中所使用的并发机制依赖于JVM的实现和CPU的 ...
- 洛谷3822 [NOI2017] 整数 【线段树】【位运算】
题目分析: 首先这题的询问和位(bit)有关,不难想到是用线段树维护位运算. 现在我们压32位再来看这道题. 对于一个加法操作,它的添加位置可以得到,剩下的就是做不超过32的位移.这样根据压位的理论. ...
- 【BZOJ1011】【HNOI2008】遥远的行星 误差分析
题目大意 给你\(n,b\),还有一个数列\(a\). 对于每个\(i\)求\(f_i=\sum_{j=1}^{bi}\frac{a_ja_i}{i-j}\). 绝对误差不超过\(5\%\)就算对. ...
- MT【310】均值不等式
(2014北约自主招生)已知正实数$x_1,x_2,\cdots,x_n$满足$x_1x_2\cdots x_n=1,$求证:$(\sqrt{2}+x_1)(\sqrt{2}+x_2)\cdots(\ ...
- windows下操作linux虚拟机映射网络驱动器中文件提示chmod权限不足解决方案
为了方便操作,linux虚拟机会通过windows下连接网络驱动器的方式共享自己的文件,对于前端来说,我想把gulp放在windows磁盘,操作虚拟机中的php文件,一来节省虚拟机磁盘大小,二来解决虚 ...