【动态规划】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 ...
随机推荐
- 运动目标前景检测之ViBe源代码分析
一方面为了学习,一方面按照老师和项目的要求接触到了前景提取的相关知识,具体的方法有很多,帧差.背景减除(GMM.CodeBook. SOBS. SACON. VIBE. W4.多帧平均……).光流(稀 ...
- [bzoj 2844]线性基+高斯消元
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2844 又用到线性基+高斯消元的套路题了,因为经过高斯消元以后的线性基有非常好的序关系,所以 ...
- netty入门代码学习
服务端代码: package com.lsp.netty; /** * @author lishupeng * @create 2017-05-27 下午 3:48 **/ import io.net ...
- 图片上传是否为空,以及类型的js验证
function check2() { var file = document.getElementsByName("file").value; if(file=="&q ...
- Flex UI刷新后保持DataGrid中的ScrollBar的位置不变
这是之前我发的一个贴子问题描述:http://q.cnblogs.com/q/53469/
- jupyter、flask、tornado、djiango安装
安装了pip包的话直接使用: 1.安装jupyter:pip install jupyter 2.安装flask: pip install flask 3.安装tornado:pip install ...
- mobius反演讲解
mobius反演的基本形式为,假设知道函数F(x)=Σf(d) d|x,那么我们可以推出f(x)=Σmiu(d)*F(x/d) d|x,另一基本形式为假设知道函数F(x)=Σf(d) x|d,那么我们 ...
- 数据安全之MD5、SHA-1、CRC32区别
crc32 — 计算一个字符串的 crc32 多项式 生成 string 参数的 32 位循环冗余校验码多项式……:这句话从英文翻译过来的,不正确,准确的说应该是这么理解: 以32位循环冗余校验多项式 ...
- UVALIVE 3562 Remember the A La Mode!
费用流 建图很简单直接上代码 #include <map> #include <set> #include <list> #include <cmath> ...
- Linux中生成Core Dump系统异常信息记录文件的教程
Linux中生成Core Dump系统异常信息记录文件的教程 http://www.jb51.net/LINUXjishu/473351.html