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的更多相关文章

  1. Railroad UVALive - 4888 记忆化搜索

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate

    UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...

  3. 【暑假】[深入动态规划]UVAlive 3983 Robotruck

     UVAlive 3983 Robotruck 题目: Robotruck   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format ...

  4. 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 ...

  5. 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 ...

  6. UVALive 5111 Soccer Teams (动态规划)

    题意:给指定数量的数字“1”,“2”,“3”……,“9”.用所有这些数字加上任意个0组成一个数,要求数能被11整除,且数的位数尽量小. 能被11整除的数有一个特点,奇数位数字之和与偶数位之和的差为11 ...

  7. UVALive 2324 Human Gene Functions(动态规划)

    题意:求出将两个字符串改成一样长度所能形成最大的相似度. 思路:这个可以说是编辑距离的一个变形,编辑距离最终状态时要两个字符串完全一致,这个就是要求长度一样,而且这个只允许插入“—”这一个字符.模仿编 ...

  8. UVALive 6486 Skyscrapers 简单动态规划

    题意: 有N个格子排成一排,在每个格子里填上1到N的数(每个只能填一次),分别代表每个格子的高度.现在给你两个数left和right,分别表示从左往右看,和从右往左看,能看到的格子数.问有多少种情况. ...

  9. HDU 2829 Lawrence(动态规划-四边形不等式)

    Lawrence Problem Description T. E. Lawrence was a controversial figure during World War I. He was a ...

随机推荐

  1. 在JS中,一切东东其实都是对象

    对象是组成JavaScript的基本单元,在JS中,一切东东其实都是对象,而且功能非常强大,它不仅风格独特,功能也与众不同. 一.引用(reference) 引用的概念是JS的基础之一,它是指向对象实 ...

  2. Codeforces Round #350 (Div. 2) A

    A. Holidays time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  3. Educational Codeforces Round 56 (Rated for Div. 2) ABCD

    题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...

  4. windows下maven打包eclipse工程

    打包过程中,可能出现的2个问题: ①.[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build ...

  5. CSS学习之float解析

    转自:http://www.w3cplus.com/css/float.html 一.float是什么? float即为浮动,在CSS中的作用是使元素脱离正常的文档流并使其移动到其父元素的“最左边”或 ...

  6. 《vue.js实战》练习---标签页组件

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)

    Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...

  8. autoKeras入门

    测试本地mnist数据集 图片只用500张,450张做train与50张test, 代码如下: # conding:utf-8 import os os.environ[' import numpy ...

  9. bzoj 1042 DP+容斥原理

    我们可以先DP预处理出W[I]代表买I的东西,每种钞票的个数 不做限制的方案数,那么对于每一组数据的限制,我们可以知道 W[S-C[I]*(D[I]+1)]C为面值,D为数量,这个代表第I种钞票一定 ...

  10. MySQL 之 foreign key

    前段回顾 create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); #解释: 类型:使用限制字段必须 ...