HDU - 1560 DNA sequence 【迭代加深】

HDU 1560 DNA sequence 【加深迭代dfs】【IDA*】 DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4172    Accepted Submission(s): 2005   Problem Description The twenty-first ce... 阅读详情

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560

关于迭代加深,可以参考这篇博客 https://blog.youkuaiyun.com/hzaukotete/article/details/81226556

迭代加深是在dfs的基础上,枚举深搜的深度 dep。

比如说 我一开始规定 dep == 1,当dfs深搜到深度为 1的时候就回溯,如果dep == 1没有找到答案,那么就让dep+1,继续从头开始深搜,当深度到2的时候就回溯。以此类推,直到找到答案时,dep就是我们要求的答案。也就是说,设置的dep是不管找没找到答案,到了规定的深度dep就回溯。

这种深搜有点类似bfs。

此外迭代加深的搜索一定要有一个比较有效的剪枝方法来减少搜索的次数。

最常用的方法是设置一个估计值,这个估计值不需要太精确,大概估计即可,如果这个估计值超出了规定的dep,那么就不必要做dfs了,直接dep++,以此来减少dfs的搜索次数。

思路:

枚举每一位可能出现的字母。

如样例1,给每一个字符串都设置一个指针,初始指向第一位,如果设置第一个字母为A,那么把所有指针指向的字母为A的指针都后移一位,然后继续枚举第二个字母。直到所有的指针都指向最末尾的时候,就能得到答案。

这里迭代深搜的估计方法也很简单,要想找到答案,设置的dep最起码要等于或者大于字符串最长的那个字符串的长度。同样的,在搜索的过程中,要想找到答案,当前步数step + 指针距离末尾最长的长度 <= dep。

#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;
const int Maxn = 4e5+10;

struct Q {
    int num[10], p, cur;
} qu[10];

int N, dep, ans;

void dfs(int step) {
    bool vis[10], mark[10];
    int maxn = 0;

    for(int i = 0; i < N; ++i) {
        maxn = max(maxn, qu[i].p-qu[i].cur);
    }
    if(maxn == 0) {
        ans = step; return;
    }
    if(maxn+step > dep) return;

    memset(vis, false, sizeof(vis));
    memset(mark, false, sizeof(mark));

    for(int i = 0; i < N; ++i) {
        if(qu[i].cur == qu[i].p) continue;
        int tmp = qu[i].num[qu[i].cur];

        if(vis[tmp]) continue;
        vis[tmp] = true;

        for(int j = 0; j < N; ++j) {
            if(qu[j].cur == qu[j].p) continue;
            if(qu[j].num[qu[j].cur] == tmp) {
                qu[j].cur++; mark[j] = true;
            }
        }
        dfs(step+1);
        if(ans != -1) return;
        for(int j = 0; j < N; ++j) {
            if(mark[j]) {
                qu[j].cur--; mark[j] = false;
            }
        }
    }
}

int main(void)
{
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &N);
        char str[10];
        for(int i = 0; i < N; ++i) qu[i].cur = qu[i].p = 0;
        for(int i = 0; i < N; ++i) {
            scanf("%s", str);
            for(int j = 0; j < strlen(str); ++j) {
                if(str[j] == 'A') qu[i].num[qu[i].p++]= 1;
                else if(str[j] == 'C') qu[i].num[qu[i].p++]= 2;
                else if(str[j] == 'G') qu[i].num[qu[i].p++]= 3;
                else if(str[j] == 'T') qu[i].num[qu[i].p++]= 4;
            }
        }
        ans = -1; dep = 1;
        while (ans == -1) {
            dfs(0); dep++;
        }
        printf("%d\n", ans);
    }
	return 0;
}

 

构建OneService:打造高效、安全、灵活的企业级数据服务平台 数据服务(OneService)作为统一的数据服务出口,实现了数据的统一市场化管理,有效地降低数据开放门槛的同时,保障了数据开放的安全。 阅读详情

相关推荐

X猫免费小说安卓逆向分析

七猫免费小说安卓逆向分析 难度(♥♥) 文章目录七猫免费小说安卓逆向分析 难度(♥♥)1. 查壳2. 抓包分析3. sign1. Frida-rpc2. Native函数分析快3点了. 睡觉~ 白天再继续写! 1. 查壳 我这边使用的是ApkTool, 拖进去显示未加壳。 2. 抓包分析 POST https://xiaoshuo.wtzw.com/api/v1/login/index HTTP/1.1 net-env: 1 channel: qm-cbcpa056_wm is-white: 0 platf

Qiled的博客 4368

C1. Increasing Subsequence (easy version)

put contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the input contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th element of a. All these integers are pa...

轻舟载君不载愁 515

kaggle注册以及数据集下载全流程

kaggle注册,数据集下载及常见问题

Arriteey的博客 2万+

HDU 1560 迭代加深 解题报告

hdu1560

onepointo 524

centos 添加新硬盘,对根目录扩容

本地安装了虚拟机,系统为centos7,使用过程中,渐渐地发现磁盘容量不够用了,于是在虚拟机中新增了一块硬盘。 进入系统内查看: sdb是新增的硬盘,大小为40g。 接下来有两种方式,一种是普通方式直接挂载。https://blog.youkuaiyun.com/qq_39314099/article/details/106200322 这样的话只有在挂载点的目录下操作才能使用到这个新硬盘,不是很理想。 还有一种方式lvm,逻辑卷管理的方式。 LVM简单介绍: 它是 Linux 下对磁盘分区进行管

qq_39314099的博客 1万+

ADS仿真设计平行定向耦合器学习笔记

ADS仿真设计平行定向耦合器

qq_36521926的博客 1万+

DNA sequence HDU - 1560

DNA sequence HDU - 1560

weixin_29216119的博客 277

多核系统核间通信(IPC)设计:从硬件机制到软件实现

在多核处理器与嵌入式系统设计中,核间通信(IPC)是实现计算资源协同工作的核心技术。其原理基于共享内存、硬件邮箱与中断等底层硬件机制,通过构建结构化的消息传递通道,实现核间高效、可靠的数据交换。该技术的核心价值在于打破多核间的壁垒,将分散的计算单元整合为有机整体,从而显著提升系统整体性能与响应能力。在工程实践中,IPC广泛应用于智能座舱、边缘AI网关、实时控制系统等场景,用于处理如仪表显示、AI推理、实时控制等复杂任务。本文聚焦于基于共享内存与VRING的消息队列实现,深入剖析缓存一致性、内存屏障等关键问题

weixin_27006759的博客 598

HDU-1560

使用IDA*:迭代dfs+剪枝 首先读取每一个字符串 然后选择最大长度作为初始深度 每次迭代+1 设置三个数组: dna序列数组 当前匹配数组 最短dna序列数组 剪枝: 剩余匹配数组的最大值小于深度-当前dna序列的长度 每次dfs都更新当前匹配数组。本题我根据大佬的代码做了一些改动 使用pos[]全局变量作为记录当前追踪过程 每次遍历完后回溯。其次就是dfs遍历的原理 逻辑一定要严谨 不能有疏漏 再接再厉吧。做了几道IDA*的题发现关键还是求最小代价作为剪枝是关键。

m0_73596392的博客 322

4、机器人建模、规划与控制及运动学知识解析

本博客深入解析了机器人建模、规划与控制的核心内容,涵盖运动学与动力学的基本原理,并结合典型应用场景探讨了移动机器人与操作器的控制策略。同时,博客还介绍了机器人技术的发展趋势及其在工业和服务领域的广泛应用。

desk3的博客 124

Codex 调试记录获取完全指南:日志查看、工具调用与实战排查教程

本文主要介绍了Codex 调试记录获取完全指南:日志查看、工具调用与实战排查教程,希望能对使用Codex的同学们有所帮助。 文章目录 1. 背景 2. 实战教程

weixin_43178406的博客 6万+

HDU 1560 sequence

题目链接 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(cytosin...

qq_43735840的博客 213

【WorkBuddy从入门到精通实战教程】使用手册 第 2 章 WorkBuddy的下载、安装、登录与更新

摘要:WorkBuddy官方下载地址为https://www.codebuddy.cn/work/,支持Mac和Windows系统,需根据设备自动匹配版本。安装时需核对发布者信息,Mac用户需拖入应用程序文件夹。登录支持微信扫码或手机号,公司电脑安装需IT授权。更新通过个人中心检查,遇到安装包损坏、登录失败或文件读写问题时,建议重新下载、检查网络/权限或联系官方支持。重要文件建议定期备份,避免直接操作原文件。(149字)

企鹅码农的专属播客,分享10多年的实战案例和经验解读,包含YOLO、RT-DETR、AI等各个工作领域。 1889

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

题意:找到一个最短的序列,使得输入的所有序列都是这个序列的子序列。输出最短序列的长度 DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 687    Accepted Submission(

2105

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

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

DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1296 Accepted Submission(s): 648 Problem Description The twenty-first century ...

qq_18952045的博客 173

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/3218107.html 构造一个串,使得它包含所有的给定DNA序列,并且要求长度最短。采用dfs策略,对于每个串定义1个指针,当全部指针为串尾时判断构造的长度,由于状态空间过大,但是又存在冗余搜索...

diansitao5461的博客 142

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

HDU - 1560:DNA sequence ( 迭代加深搜索基础题 )

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(cytosi...

橘嘉禾 364

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

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(thy...

weixin_30469895的博客 133

hdu 1560 DNA sequence(迭代加深)​​​​​​​

题目链接:hdu 1560 DNA sequence 题意:构造一个最短的字符串,使得给出的n个字符串(只包含ACGT)都是这个字符串的字串(可以不连续),输出字符串的长度。 思路:最短为n个字符串中最长串的长度,最长为n个字符串长度之和,在这个范围内dfs。具体dfs内容见代码注释 #include&lt;cstdio&gt; #include&lt;cstring&gt; #inc...

yz467796454的博客 197

hdu 1560 DNA sequence(迭代加深搜索)(经典题)

题意: 从n个串中找出一个最短的公共串,,该公共串对于n个字符串不要求连续,即只要保持相对顺序就好。 思路: 用迭代加深搜索,所谓迭代加深搜索,就是限制DFS的深度,若搜不到答案,则加深深度,重新搜索,这样就防止了随着深度不断加深而进行的盲目搜索,而且,对于这种求最短长度之类的题目,只要找到可行解,即是最优解了。同时注意剪枝,每次DFS的时候,都要判断一下,当前的深度+最少还有加深的深度是否大于限制的长度,若是,则退回上一层状态。

sunny crazy 2283

HDU-#1560 DNA sequence(迭代加深搜)

题目大意: 解题思路:

VoidSun 673

HDU - 1560 DNA sequence 迭代加深搜索

DNA sequenceDescriptionThe 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),

ywq12138的博客 452

hdu1560 DNA sequence (迭代加深搜索)

题意:从n个串中找出一个最短的公共串(也许应该说序列吧,因为不要求连续,即只要保持相对顺序就好)。 分析:一开始自以为是爆搜开了,因为已经知道了可以用迭代加深搜索。所谓迭代加深搜索,就是每次都限制了DFS的深度,若搜不到答案,则加深深度,继续搜索,这样就防止了随着深度不断加深而进行的盲目搜索,而且,对于这种求最短长度之类的题目,只要找到可行解,即是最优解了。所以就这样敲完代码了,敲完之后,悲剧T...

weixin_30580943的博客 100

HDU1560 DNA sequence【IDA*】

DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5921 Accepted Submission(s): 2694 Problem Description The twenty-first century...

海岛Blog 672
上一篇: HDU - 3533 Escape 【BFS】
下一篇: ZOJ - 2477 Magic Cube 【迭代加深+模拟】
Y390d
博客等级 码龄9年 11粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值