DNA sequence HDU - 1560(IDA*,迭代加深搜索)
题目大意:有n个DNA序列,构造一个新的序列,使得这n个DNA序列都是它的子序列,然后输出最小长度。
题解:第一次接触IDA*算法,感觉~~好暴力!!思路:维护一个数组pos[i],表示第i个串该匹配第pos[i]个元素。一共有四种可能,A,C,G,T,依次枚举,如果说A和n个串中的第j个串匹配,就直接就pos[j]++就行了,然后进入下一层,如果说最终没有成功构造,还有进行回溯,所以每一层我们都要维护一个数组来记录pos的值方便回溯。
具体实现和注释都在code中了,应该不难理解.
code:
#include<bits/stdc++.h>
using namespace std;
const int N=;
int len[N];
string s[N];
int n;
string st="AGCT";
int pos[N];//表示第i个字符串匹配该第pos[i]个位置
int get(){//用来表示当前最少要走多少部步
int ans=;
for(int i=;i<=n;i++)
ans=max(ans,len[i]-pos[i]);
return ans;
}
bool dfs(int step,int depth){
if(step+get()>depth) return ;//长度不够
if(!get()) return ;//全部匹配成功
int temp[];//回溯的时候会用到
for(int i=;i<=n;i++) temp[i]=pos[i];
for(int i=;i<;i++){//A C G T,依次枚举匹配
bool flag=;//用来记录第j个string,pos[j]处的字符串是否匹配成功.
for(int j=;j<=n;j++){
if(s[j][pos[j]]==st[i]){
flag=;
pos[j]++;
}
}
if(flag){//flag=1,说明至少有一个串和st[i]匹配.
if(dfs(step+,depth)) return ;
for(int i=;i<=n;i++) pos[i]=temp[i];
}
}
return ;
}
int main(){
ios::sync_with_stdio();
int t;
cin>>t;
while(t--){
memset(len,,sizeof len);
memset(pos,,sizeof pos);
cin>>n;
string c;
int maxn=;
for(int i=;i<=n;i++){
cin>>s[i];
len[i]=s[i].size();
maxn=max(maxn,len[i]);
}
while(){
if(dfs(,maxn)) break;
maxn++;
}
cout<<maxn<<endl;
}
return ;
}
DNA sequence HDU - 1560(IDA*,迭代加深搜索)的更多相关文章
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索
迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...
- DNA sequence HDU - 1560
DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- IDA*、剪枝、较难搜索、扫描——DNA sequence HDU - 1560
万恶之源 翻译 题意就是给出N个DNA序列,要求出一个包含这n个序列的最短序列是多长 这是一道搜索题,为什么呢?从样例可以感受到,我们应该从左往右"扫描",从n个DNA序列中取出某 ...
- G - DNA sequence HDU - 1560
题目链接: https://vjudge.net/contest/254151#problem/G AC代码: #include<iostream> #include<cstring ...
- POJ2286 The Rotation Game[IDA*迭代加深搜索]
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 6325 Accepted: 21 ...
- HDOJ-1560(迭代加深搜索问题)
DNA sequence HDOJ-1560 *本题是迭代加深搜索问题,主要是要理解题目,题目中一定是有解的,所以为了找最小的解,可以从小的搜索深度开始逐渐增加. *这里有个技巧就是,如果本次指定开始 ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 1560 DNA sequence (迭代加深搜索)
The twenty-first century is a biology-technology developing century. We know that a gene is made of ...
随机推荐
- cmdb简介
目录: 1.为啥要做cmdb
- Building Applications with Force.com and VisualForce (DEV401) (三):Application Essential:Building Your Data Model
Dev 401-003:Application Essential:Building Your Data Model Object Relationships1.Link two objects- P ...
- OpenCV-Python 特征匹配 + 单应性查找对象 | 四十五
目标 在本章节中,我们将把calib3d模块中的特征匹配和findHomography混合在一起,以在复杂图像中找到已知对象. 基础 那么我们在上一环节上做了什么?我们使用了queryImage,找到 ...
- TensorBoard中HISTOGRAMS和DISTRIBUTIONS图形的含义
前言 之前我都是用TensorBoard记录训练过程中的Loss.mAP等标量,很容易就知道TensorBoard里的SCALARS(标量)(其中横纵轴的含义.Smoothing等). 最近在尝试模型 ...
- Python第十章-模块和包
模块和包 我们以前的代码都是写在一个文件中, 而且代码也比较短. 假设我们现在要写一个大的系统, 不可能把代码只写到一个文件中, 迫切想把代码写到不同的文件中, 并且能够在一个文件使用另一个文件中代码 ...
- iOS 编译过程原理(2)
一.前言 <iOS编译过程的原理和应用>文章介绍了 iOS 编译相关基础知识和简单应用,但也很有多问题都没有解释清楚: Clang 和 LLVM 究竟是什么 源文件到机器码的细节 Link ...
- LeetCode(一) jump game
一. 1. #include<iostream> #include<cmath> using namespace std; bool CanJump(int n[],int n ...
- python编程心得(1)
1.创建字典 字典名 = {键名1:键值1,键名2:键值2,...} sanguo = {"诸葛亮草船借箭":"满载而归","关公赴会 ...
- 1011 World Cup Betting (20 分)
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...
- 在docker中部署redis主从配置
环境说明: 阿里云服务器 Ubuntu 16.04 docker 1.拉取Redis镜像 docker pull redis 2.配置Redis启动配置文件,此处我创建一个专用目录,存放Redis相关 ...