http://lightoj.com/volume_showproblem.php?problem=1051

对于每个位置,设dfs(cur, one, two)表示前i个字母,拥有辅音字母one个,元音字母two个的情况。

目标是使得cur移动到结尾,这样就是能产生good串。然后超时

记忆化搜索。

比如前面的字符串是

JFLKAJS??FHJADJFA?

我不管前面的问号变了什么,也不管前面的东西是什么情况,假如,假如我的最后一个问号是变了元音,那么它下一个状态肯定

就是dfs(cur + 1, 0, two + 1),也就是辅音变成了0个。这样会使得很多状态重复了。所以记录一下就好。

JFLKAJS??FHJADJFA   ?   JFIJIJASLH??SDUAFHK??,也就是考虑现在空格分开的那个问号,假如我是把它变了元音,

它的连续的元音是2个,辅音是0个,这是固定了的状态了,如果搜索过的话,后面的已经不用枚举了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
bool is[];
int dp[ + ][ + ][ + ];
int DFN, lenstr;
char str[];
bool good, bad;
void dfs(int cur, int one, int two) {
if (dp[cur][one][two] == DFN) return;
dp[cur][one][two] = DFN;
if (bad && good) return;
if (one == || two == ) {
bad = true;
return;
}
if (cur == lenstr + ) {
good = true;
return;
}
if (str[cur] == '?') {
dfs(cur + , one + , );
dfs(cur + , , two + );
} else {
if (is[str[cur]]) {
dfs(cur + , , two + );
} else dfs(cur + , one + , );
}
}
void work() {
scanf("%s", str + );
lenstr = strlen(str + );
++DFN;
good = false, bad = false;
dfs(, , );
static int f = ;
if (good && bad) {
printf("Case %d: MIXED\n", ++f);
} else if (bad) {
printf("Case %d: BAD\n", ++f);
} else {
printf("Case %d: GOOD\n", ++f);
}
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
is['A'] = is['E'] = is['I'] = is['O'] = is['U'] = true;
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

1051 - Good or Bad DFS 记忆化搜索的更多相关文章

  1. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  2. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  3. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  4. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  5. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  6. 8636 跳格子(dfs+记忆化搜索)

    8636 跳格子 该题有题解 时间限制:2457MS  内存限制:1000K提交次数:139 通过次数:46 题型: 编程题   语言: G++;GCC Description 地上有一个n*m 的数 ...

  7. poj1088-滑雪 【dfs 记忆化搜索】

    http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 79806 ...

  8. DFS——>记忆化搜索——>动态规划

    以洛谷P1802  5倍经验日 为例 https://www.luogu.org/problem/show?pid=1802 题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却 ...

  9. POJ 1191 棋盘分割 【DFS记忆化搜索经典】

    题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

随机推荐

  1. 例子:两个表根据productID合并

  2. SpringCloud遇到的坑

    1. 今天使用Feign 调用其他项目,结果一直跳转到断路器,跟踪发现是接口响应时间较长,解决方案 解决:# 在 Feign 模块中,单独设置这个超时时间不行,还要额外设置 Ribbon 的超时时间, ...

  3. A nonrecursive list compacting algorithm

    A nonrecursive list compacting algorithm Each Erlang process has its own stack and heap which are al ...

  4. C++文件IO操作的简单示例

    CppIODemo1.cpp #include <iostream> #include <fstream> #include <chrono> #define IN ...

  5. tomcat 部署项目的多种方式

    项目放在tomcat webapps也不会加载两次 下面可以指定项目名称及path   加载war   部署war包  后面不用加war的后缀 <Host appBase="D:/pr ...

  6. 牛客练习赛42 E.热爆了

    这可能是全场最长的一份代码 问的其实是对于关键点的斯坦纳树大小 考虑补集转化,不合法的点就是它的子树中没有关键点的点和斯坦纳树根的祖先 树根不难求,关键点中dfs序最大最小点的LCA就是了 问题在前者 ...

  7. YTU 2427: C语言习题 整数排序

    2427: C语言习题 整数排序 时间限制: 1 Sec  内存限制: 128 MB 提交: 391  解决: 282 题目描述 用指向指针的指针的方法对n个整数排序并输出.要求将排序单独写成一个函数 ...

  8. 启动tomcat时,经常遇到的问题 8080 端口被占用

    启动tomcat失败时,弹出的窗口如上,说明8080被某个进程占用了 需要把占用8080端口的进程给kill掉 Win+R 输入运行cmd 打开命令提示符cmd.exe netstat -ano|fi ...

  9. fastText(三):微博短文本下fastText的应用(二)

    上一篇讲到,fastText在训练数据中过拟合的问题.接下来将介绍一些提高fastText泛化能力的尝试. 模型泛化使用过fastText的人,往往会被它的很多特性征服,例如训练速度.兼具word e ...

  10. MySQL主从详细安装步骤

    网站: 程序在:web服务器192.168.1.100上面 数据库在:MySQL服务器192.168.1.123上面 实现目的:增加一台MySQL备份服务器(192.168.1.124),作为MySQ ...