DNA sequence

HDOJ-1560

*本题是迭代加深搜索问题,主要是要理解题目,题目中一定是有解的,所以为了找最小的解,可以从小的搜索深度开始逐渐增加。

*这里有个技巧就是,如果本次指定开始迭代搜索的深度之后没有找到解,那么需要用一个临时数组将每个字符串中已经匹配好的字符数存起来。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int n;
char chain[8][5];
int p[8];//表示第i个字符串已经匹配好的字符数
int charNums[8];//表示第i个字符串的长度
bool flag=false;
char dna[4]={'A','C','G','T'};
int remainChars(){
int maxs=0;
for(int i=0;i<n;i++){
maxs=max(maxs,charNums[i]-p[i]);
}
return maxs;
}
void dfs(int depth){
if(flag)
return;
if(remainChars()==0){//所有字符串中带匹配的字符数都为0个的话,则搜索结果找到了
flag=true;
return;
}
if(remainChars()>depth)
return; int temp[8];
for(int i=0;i<n;i++){//开一个辅助数组,先将每个字符串已经匹配好的字符数存起来,如果后序没有找到正确结果再复制回去
temp[i]=p[i];
}
for(int j=0;j<4;j++){
bool find=false;
for(int i=0;i<n;i++){
if(chain[i][p[i]]==dna[j]){
p[i]++;//对于每一个属于dna的字符,对于每个n个字符串中待匹配位置的字符和它相符,待匹配位置都加一
find=true;
}
}
if(find){ dfs(depth-1);
if(flag){//如果递归回来以后发现找到了,则
return;
}
for(int i=0;i<n;i++){
p[i]=temp[i];
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--){
cin>>n;
int maxs=0;//表示初始时就需要传入迭代搜索里面的最小搜索深度
flag=false;
memset(p,0,sizeof(p));
//memset(chain,'\0',sizeof(chain));//
for(int i=0;i<n;i++){
cin>>chain[i];
int len=strlen(chain[i]);
charNums[i]=len;
maxs=max(maxs,len);
}
while(1){//对于每个循环,每次迭代深度都增加一,知道找到结果。
dfs(maxs);
if(flag){
cout<<maxs<<endl;
break;
}
maxs++;
}
}
//system("pause");
return 0;
}

HDOJ-1560(迭代加深搜索问题)的更多相关文章

  1. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  2. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  3. HDU 1560 DNA sequence (迭代加深搜索)

    The twenty-first century is a biology-technology developing century. We know that a gene is made of ...

  4. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  5. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  6. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  7. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  8. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  9. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神

    题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...

  10. C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

    此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...

随机推荐

  1. poj1066 线段相交简单应用(解题报告)

    #include<stdio.h> #include<math.h> const double eps=1e-8; int n; struct Point { double x ...

  2. zoj3593One Person Game (扩展欧几里德)

    There is an interesting and simple one person game. Suppose there is a number axis under your feet. ...

  3. Codeforces Round #582 (Div. 3) C. Book Reading

    传送门 题意: 给你n,k.表示在[1,n]这个区间内,在这个区间内找出来所有x满足x%k==0,然后让所有x的个位加到一起(即x%10),输出. 例如:输入10 2 那么满足要求的数是2 4 6 8 ...

  4. Atcoder ABC155_C中有关c++ STL map的用法

    题目:https://atcoder.jp/contests/abc155/tasks/abc155_c 这道题的题意是给我们n个string,让我们统计每个string出现的次数,并输出次数最多的一 ...

  5. c#小灶——9.算术运算符

    算数运算符用来在程序中进行运算. 首先,了解最简单的加(+)减(-)乘(*)除(/)运算符: 举例 int a = 1; int b = 2; int c = a + b; Console.Write ...

  6. 009.NET5_程序的发布运行

    发布 相差了web.config文件 脚本启动 cmd,进入程序根目录. 带参启动 其实,最终与web.config中效果一样

  7. github gist 无法访问

    转自这里 以管理员身份在hosts文件: Windows: C:\Windows\System32\drivers\etc Ubuntu: /etc/hosts 添加: 192.30.253.118 ...

  8. React PureComponent All In One

    React PureComponent All In One import React, { // useState, // useEffect, // Component, PureComponen ...

  9. How to enable a local HTTPS website in macOS

    How to enable a local HTTPS website in macOS local SSL certificate http://loclahost:8888 https://loc ...

  10. Scratch 游戏开发

    Scratch 游戏开发 可视化少儿编程 https://scratch.mit.edu/ Scratch Desktop https://scratch.mit.edu/download https ...