Railroad

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

A train yard is a complex series of railroad tracks for storing, sorting, or loading/unloading railroad cars. In this problem, the railroad tracks are much simpler, and we are only interested in combining two trains into one.

Figure 1: Merging railroad tracks.

The two trains each contain some railroad cars. Each railroad car contains a single type of products identified by a positive integer up to 1,000,000. The two trains come in from the right on separate tracks, as in the diagram above. To combine the two trains, we may choose to take the railroad car at the front of either train and attach it to the back of the train being formed on the left. Of course, if we have already moved all the railroad cars from one train, then all remaining cars from the other train will be moved to the left one at a time. All railroad cars must be moved to the left eventually. Depending on which train on the right is selected at each step, we will obtain different arrangements for the departing train on the left. For example, we may obtain the order 1,1,1,2,2,2 by always choosing the top train until all of its cars have been moved. We may also obtain the order 2,1,2,1,2,1 by alternately choosing railroad cars from the two trains.

To facilitate further processing at the other train yards later on in the trip (and also at the destination), the supervisor at the train yard has been given an ordering of the products desired for the departing train. In this problem, you must decide whether it is possible to obtain the desired ordering, given the orders of the products for the two trains arriving at the train yard.

Input

The input consists of a number of cases. The first line contains two positive integers N1 N2 which are the number of railroad cars in each train. There are at least 1 and at most 1000 railroad cars in each train. The second line contains N1 positive integers (up to 1,000,000) identifying the products on the first train from front of the train to the back of the train. The third line contains N2 positive integers identifying the products on the second train (same format as above). Finally, the fourth line contains N1+N2 positive integers giving the desired order for the departing train (same format as above).

The end of input is indicated by N1 = N2 = 0.

Output

For each case, print on a line possible if it is possible to produce the desired order, or not possible if not.

Sample Input

3 3
1 2 1
2 1 1
1 2 1 1 2 1
3 3
1 2 1
2 1 2
1 1 1 2 2 2
0 0

Sample Output

possible
not possible

Source

HDOJ开放式进阶训练(11)——全程测试
 
题解:记忆化搜索,记录状态
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int n,m;
int a[],b[],c[];
bool vis[][];
bool dfs(int x,int y,int k)
{
if (k==n+m+) return ;
if(vis[x][y]) return ;
vis[x][y]=;
if (x<=n && a[x]==c[k] && dfs(x+,y,k+))
return ;
if (y<=m && b[y]==c[k] && dfs(x,y+,k+))
return ; return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if (n== && m==) break;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=m;i++) scanf("%d",&b[i]);
for(int i=;i<=n+m;i++)scanf("%d",&c[i]);
memset(vis,,sizeof(vis));
if (dfs(,,)) printf("possible\n");
else printf("not possible\n");
}
return ;
}

HDU 3779 Railroad(记忆化搜索)的更多相关文章

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

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

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

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

  3. hdu 4826(dp + 记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...

  4. hdu---(3779)Railroad(记忆化搜索/dfs)

    Railroad Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)

    题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...

  6. hdu 4345 Permutation 记忆化搜索

    思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...

  7. POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. hdu1078 记忆化搜索

    /* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...

  9. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

随机推荐

  1. sgu 101 Domino 解题报告及测试数据

    101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB 题解: 求多米诺骨牌按照一定方式放置能否使相邻的位置 ...

  2. maven 介绍(一)

    本文内容主要摘自:http://www.konghao.org/index 内部视频                              http://www.ibm.com/developer ...

  3. FFmpeg从入门到精通——进阶篇,SEI那些事儿

    前言 在直播应用的开发过程中,如果把主播端消息事件传递到观众端,一般会以Instant Messaging(即时通讯)的方式传递过去,但因为消息分发通道和直播通道是分开的,因此消息与直播音视频数据的同 ...

  4. 20145328 《Java程序设计》实验二实验报告

    20145328 <Java程序设计>实验二实验报告 实验名称 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 ...

  5. .NET数据库编程求索之路--1.引子

    转载:[ 夏春涛 email: xchuntao@163.com  blog: http://www.cnblogs.com/SummerRain ] 长期做.NET MIS系统开发,打交到最多还是数 ...

  6. cogs 547:[HAOI2011] 防线修建

    ★★★☆   输入文件:defense.in   输出文件:defense.out   简单对比 时间限制:1 s   内存限制:128 MB 题目描述: 近来A国和B国的矛盾激化,为了预防不测,A国 ...

  7. ssh-keygen 的 详解

    为了让两个Linux机器之间使用ssh不需要用户名和密码.所以采用了数字签名RSA或者DSA来完成这个操作. 模型分析 假设 A (192.168.20.59)为客户机器,B(192.168.20.6 ...

  8. MySQL表损坏修复【Incorrect key file for table】

    今天机房mysql服务器异常关机,重新启动后报错如下: -- :: [ERROR] /usr/local/mysql/bin/mysqld: Incorrect key file for table ...

  9. RDB

    在运行情况下, Redis 以数据结构的形式将数据维持在内存中, 为了让这些数据在 Redis 重启之后仍然可用, Redis 分别提供了 RDB 和 AOF 两种持久化模式. 在 Redis 运行时 ...

  10. LCS最长共同子序列

    2017-09-02 15:06:57 writer:pprp 状态表示: f(n,m)表示s1[0..n]和s2[0..m]从0开始计数,最终结果是f(N-1,M-1)考虑四种情况: 1/ s1[n ...