【动态规划】UVALive - 4888 - Railroad
f(i,j)表示从A序列前面取i个,从B序列前面取j个时,能否拼成C序列。转移自行脑补。
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+N2positive 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
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,a[1010],b[1010],c[2010];
bool f[1010][1010];
int main()
{
while(1)
{
scanf("%d%d",&n,&m);
if((!n)&&(!m))
break;
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
for(int i=1;i<=m;++i)
scanf("%d",&b[i]);
for(int i=1;i<=n+m;++i)
scanf("%d",&c[i]);
memset(f,0,sizeof(f));
f[0][0]=1;
for(int i=0;i<=n;++i)
for(int j=0;j<=m;++j)
{
if(i>0&&j>0)
{
if((f[i-1][j] && a[i]==c[i+j]) || (f[i][j-1] && b[j]==c[i+j]))
f[i][j]=1;
}
else if(i==0&&j>0)
{
if(f[i][j-1] && b[j]==c[i+j])
f[i][j]=1;
}
else if(i>0&&j==0)
{
if(f[i-1][j] && a[i]==c[i+j])
f[i][j]=1;
}
}
puts(f[n][m] ? "possible" : "not possible");
}
return 0;
}
【动态规划】UVALive - 4888 - Railroad的更多相关文章
- Railroad UVALive - 4888 记忆化搜索
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate
UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...
- 【暑假】[深入动态规划]UVAlive 3983 Robotruck
UVAlive 3983 Robotruck 题目: Robotruck Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format ...
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- UVALive 5111 Soccer Teams (动态规划)
题意:给指定数量的数字“1”,“2”,“3”……,“9”.用所有这些数字加上任意个0组成一个数,要求数能被11整除,且数的位数尽量小. 能被11整除的数有一个特点,奇数位数字之和与偶数位之和的差为11 ...
- UVALive 2324 Human Gene Functions(动态规划)
题意:求出将两个字符串改成一样长度所能形成最大的相似度. 思路:这个可以说是编辑距离的一个变形,编辑距离最终状态时要两个字符串完全一致,这个就是要求长度一样,而且这个只允许插入“—”这一个字符.模仿编 ...
- UVALive 6486 Skyscrapers 简单动态规划
题意: 有N个格子排成一排,在每个格子里填上1到N的数(每个只能填一次),分别代表每个格子的高度.现在给你两个数left和right,分别表示从左往右看,和从右往左看,能看到的格子数.问有多少种情况. ...
- HDU 2829 Lawrence(动态规划-四边形不等式)
Lawrence Problem Description T. E. Lawrence was a controversial figure during World War I. He was a ...
随机推荐
- linux下解压,压缩命令大全---方便新手查看
本文最后讲述如何在打包时排除某些文件 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --- ...
- HDU 2084 DP经典例子---数塔问题
http://acm.hdu.edu.cn/showproblem.php?pid=2084 #include "iostream" #include "cstdio&q ...
- Drupal7导入语言包
下载语言包 然后把包存放在profiles\standard\translations目录下,安装. 如果出现如下错误 1.错误有MySql数据库默认引擎innoDB引起,只要mysql\bin\my ...
- COGS2085 Asm.Def的一秒
时间限制:1 s 内存限制:256 MB [题目描述] “你们搞的这个导弹啊,excited!” Asm.Def通过数据链发送了算出的疑似目标位置,几分钟后,成群结队的巡航导弹从“无蛤”号头顶掠过 ...
- syntax error near unexpected token `then'问题的解决
http://blog.csdn.net/gongmin856/article/details/7690917 #!/bin/bash #if program test echo 'a:' read ...
- VC++/MFC中调用CHM帮助文档的方法
转载:http://blog.csdn.net/hediping9811/article/details/23341387 (1)用Word编辑好帮助文档,并保存为网页格式,如mhtml格式. (2) ...
- 6.shell判断语句
[ condition ](注意condition前后要有空格),可以使用$?验证(0为true,>1为false) 两个整数的比较:=:字符串比较-lt:小于-gt:大于-le:小于等于-ge ...
- jQuery插件--zTree中点击节点实现页面跳转时弹出两个页面的问题
这是第一次使用zTree,所以在使用之前我要先写一个demo来学习一下.我们要注意的是,zTree是一个jQuery插件,所以我们在导入zTree的js文件之前要先导入jQuery的js文件. 我们先 ...
- redis特性与使用场景
一.8大特性 1.速度快 数据存储在内存,可达到10万OPS 2.可持久化,断电不丢数据 所有数据保存在内存中,对数据的更新异步的保存在硬盘中 3.多种数据结构 字符串.哈希.列表.集合.有序集 合位 ...
- foreach 与 Linq的 Select 效率问题
Resharper 是一个非常强大的C#编程辅助工具,有着非常强的提示功能,代码纠正,代码简化等等 在编码过程中注意到这么一件事,可能是大家经常会遇到的: 遍历某个集合,然后经过处理生成另外一个集合, ...