Railroad UVALive - 4888 记忆化搜索
做这题的时候我第一感觉是直接dfs的dp,用dp[i][j]表示第一个数组处理到第i位数字,第二个数组处理到第j个数字,能否组合出第三个数组的前cur个数字。
转移的话,就是试试第i为能否匹配第cur个了,如果可以就直接搜,不可以就看看第j位,都不可以就false
这样转移起来我觉得很明朗,实在想不到怎么用数组来dp。
#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>
int n1, n2;
const int maxn = + ;
int a[maxn], b[maxn], c[maxn * ];
int vis[maxn][maxn], DFN, dp[maxn][maxn];
bool dfs(int cur, int pos1, int pos2) {
if (cur == n1 + n2 + ) return true;
if (vis[pos1][pos2] == DFN) return dp[pos1][pos2];
vis[pos1][pos2] = DFN;
if (a[pos1] == c[cur]) {
if (dfs(cur + , pos1 + , pos2)) {
dp[pos1][pos2] = true;
return true;
}
}
if (b[pos2] == c[cur]) {
if (dfs(cur + , pos1, pos2 + )) {
dp[pos1][pos2] = true;
return true;
}
}
dp[pos1][pos2] = false;
return false;
}
void work() {
++DFN;
for (int i = ; i <= n1; ++i) scanf("%d", &a[i]);
for (int i = ; i <= n2; ++i) scanf("%d", &b[i]);
a[n1 + ] = b[n2 + ] = inf;
for (int i = ; i <= n1 + n2; ++i) scanf("%d", &c[i]);
if (dfs(, , )) {
printf("possible\n");
} else printf("not possible\n");
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d%d", &n1, &n2) != EOF && (n1 + n2)) work();
return ;
}
Railroad UVALive - 4888 记忆化搜索的更多相关文章
- HDU 3779 Railroad(记忆化搜索)
Railroad Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Subm ...
- UVALive 6470 Chomp --记忆化搜索
题意:给一个只有三行的方块阵(横向最多100个),然后p,q,r分别代表第1,2,3层的方格数,两人轮流去掉一个格子,此时这个格子的右上方都会被去掉,面临只剩最左下角的一个格子的状态的人输,问先手能否 ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- loj 1044(dp+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...
- DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...
- HDU1978 记忆化搜索
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- Django_model进阶
Django-model进阶 QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Ent ...
- openssl生成公钥私钥对 加解密
在计算机软件开发世界中,编程语言种类极多,数据在各种语言的表现形式可能有所差异,但数据本身的处理可能,或者说本质上是完全一样的:比如数据在某个算法中的运算过程是一样的.在这里,我以加密与解密来作为例子 ...
- HDU1525 Euclid's Game
Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtra ...
- bzoj 1127 [POI2008]KUP——思路(悬线法)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1127 大于2*K的视为不能选的“坏点”.有单个格子满足的就直接输出. 剩下的都是<K的 ...
- NOIP2000提高组(RQNOJ314)方格取数
题目描述 设有N*N的方格图(N<=10,我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0.如下图所示(见样例): 某人从图的左上角的A 点出发,可以向下行走,也可以向右走,直到到达 ...
- MVC 登录后重定向回最初请求的 URL FormsAuthentication.RedirectFromLoginPage
在传统的Asp.net webForm 中如果使用 Form身份验证.登录后重定向到最初请求的页面只需使用 FormsAuthentication.RedirectFromLoginPage 但在MV ...
- 安全运维之关于个人ip定位与网站监控的分析
场景: 后台:有人盗刷我的短信接口.小偷偷我手机.无良黑客黑我网站 前台:发个欺骗链接或者说我在网上举报谁谁谁附带一个跳转url获取对方ip....... How to solve: ...
- CodeForces 748C Santa Claus and Robot (思维)
题意:给定一个机器人的行走路线,求最少的点能使得机器人可以走这样的路线. 析:每次行走,记录一个方向向量,每次只有是相反方向时,才会增加一个点,最后再加上最后一个点即可. 代码如下: #pragma ...
- Android 4.2开发环境搭建
一.工具 jdk1.7; eclipse 4.3(for java ee); Android SDK; 二.安装JDK并配置 安装略,配置如下: 右击 “我的电脑”->属性->高级系统设置 ...
- CodeForces - 566D Restructuring Company 并查集的区间合并
Restructuring Company Even the most successful company can go through a crisis period when you have ...