【动态规划】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 ...
随机推荐
- poj2814-拨钟问题-C语言-枚举算法
#include <stdio.h> #include <stdlib.h> /* 首先,我们考虑用长度为9的数组表示表盘的状态以及调表的操作,终止的条件是表盘状态数组所有元素 ...
- activity栈清空
http://blog.csdn.net/swjtuxu/article/details/26163737
- ireport写sql语句的按钮在哪
- 转:mybatis 高级结果映射(http://blog.csdn.net/ilovejava_2010/article/details/8180521)
高级结果映射 MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程 ...
- jQuery通过CSS()方法给指定的元素同时设置多个样式
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax ...
- linux下面which whereis find locate的使用
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有时很长时间不会用到,当要用的时候经常弄混了,所以放到这里方便使用. which ...
- Chubby lock service for distributed system
Chubby lock service在分布式系统中的应用 Chubby lock service在分布式系统中提供粗粒度的锁服务, 以及可靠的存储. 相比高性能, 设计的重点在于高可靠性和高可用性. ...
- 让button居中显示的的标签
<center> <input type="button" class="buttoncls" style="width:80px& ...
- python特有的协程
#转载请联系 什么是协程呢? 线程包含在进程里面,协程包含在线程里面.协程也是和进程.线程一样,可以实现多任务.协程的切换开销比线程更小,不需要保存和恢复线程的状态.最通俗易懂的说法就是,协程是就是一 ...
- Selenium2+python自动化32- 测试报告的易读性【转载】
前言 前一篇已经介绍了报告的生成方法,本篇小编优化一下测试报告,使测试报告便于大多数阅读.虽然在我们在测试用例开发时为每个用例添加了注释,但测试报告一般是给非测试人员阅读的,如果能在报告中为每一个测试 ...