[IDDFS] HDU 1560
来源:互联网 发布:arena软件 编辑:程序博客网 时间:2024/06/05 16:29
给出N个DNA序列,要求出一个包含这n个序列的最短序列是多长
因为是求最小,所以用迭代加深,直接防止深搜搜索得过头
迭代加深就是逐渐增加搜索的范围。
最初我们只搜索maxd的范围,当maxd内没有我们需要的答案时,我们将maxd+1,而后继续搜索。
#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <string>using namespace std;inline int max ( int a, int b ) { return a > b ? a : b; }int n; //字符串个数int ans; //最终结果int depth; //最大搜索深度char str[ 10 ][ 10 ];char DNA[ 4 ] = {'A', 'T', 'C', 'G'};// cnt: 当前已经有了多长的字符串// len[]: n个字符串已经搜素到了的位置void dfs ( int cnt, int len[] ) { if ( cnt > depth ) return; //求每个串还剩下多少 int remain = 0; for ( int i = 0; i < n; ++i ) remain = max ( strlen ( str[ i ] ) - len[ i ], remain ); if ( remain == 0 ) { ans = cnt; return; } if ( remain + cnt > depth ) //该限定深度搜索不到 return; for ( int i = 0; i < 4; ++i ) { int pos[ 10 ]; int flag = 0; //匹配到至少一个就能往下搜索 for ( int j = 0; j < n; ++j ) { //匹配到的len+1 if ( str[ j ][ len[ j ] ] == DNA[ i ] ) flag = 1, pos[ j ] = len[ j ] + 1; //没匹配到的len不变 else pos[ j ] = len[ j ]; } if ( flag ) dfs ( cnt + 1, pos ); if ( ans != -1 ) return; }}void IDdfs () { ans = -1; int pos[ 10 ] = {0}; while ( ans == -1 ) { dfs ( 0, pos ); //迭代加深搜索算法限制每一次搜索的深度最大为depth depth++; }}int main () { int t; scanf ( "%d", &t ); while ( t-- ) { scanf ( "%d", &n ); depth = 0; for ( int i = 0; i < n; ++i ) { scanf ( "%s", str[ i ] ); depth = max ( depth, strlen ( str[ i ] ) ); } IDdfs (); printf ( "%d\n", ans ); } return 0;}
阅读全文
0 0
- [IDDFS] HDU 1560
- UVA12558->IDDFS
- uva12558 埃及分数 IDDFS
- 埃及分数问题-IDDFS
- 【IDDFS】【vijos 1350】C数列
- POJ 3134 - Power Calculus (IDDFS)
- IDDFS--UESTC - 577 分数拆分
- IDDFS--UVA - 1374 Power Calculus
- poj 3134 Power Calculus iddfs(迭代深搜)
- IDDFS--UVA - 11212 Editing a Book
- Luogu 2346/codevs 1004(IDDFS)
- IDDFS(Iterative deepening depth-first search)的Java实现
- [codevs 1288] 埃及分数 [IDdfs 迭代加深搜索 ]
- uva12558(埃及分数--难)(IDDFS入门题)
- 迭代加深、IDDFS解决埃及分数问题
- 埃及分数问题 迭代加深搜索(IDDFS)
- 一中OJ #1193 倒水问题[1] | IDDFS-迭代加深 + 二分猜答案 | 解题报告
- hdu 1560
- HDOJ 1407 测试你是否和LTC水平一样高
- Angular 插值绑定
- jdbc连接数据库time_zone问题
- C语言局部变量
- css盒子模型
- [IDDFS] HDU 1560
- 设计模式-6大设计原则
- 从PHP中的文件内容获取完全限定的类名
- Windows下安装Python和pip终极教程
- (二)心累
- ios-蓝牙简介
- 安装Android-studio遇到Gradle sync failed:和出现building“project name”gradle project info
- Android的ROM制作
- NOIP 2017 完挂记