hdu3779 Railroad

Railroad

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 70    Accepted Submission(s): 24

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 N 1  N 2  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 N 1  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 N 2  positive integers identifying the products on the second train (same format as above). Finally, the fourth line contains N 1  +N 2  positive integers giving the desired order for the departing train (same format as above).

The end of input is indicated by N 1  = N 2  = 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

昨天的杭电的比赛做的比较悲催,感觉比较烦躁…… 这题交了 8 次才想到优化的方法才过的,题意是这样的,有两个铁路,合成一个铁路,在两个铁路上原来 都有一些车,分别是 n1 辆和 n2 辆,然后告诉你这两条铁路上原来车的顺序,再给你希望合成一条铁路后的车的顺序,问你是否可行 ……

ps:为神马看到铁路总想起栈??呃 ……

显然dfs …… 设一个递归函数 f(int t1,int t2,int t) t1 是第一条铁路进第 t1 辆火车, t2 是第二条铁路进第 t2 辆火车, t 是合并的那条铁路进了几辆火车,所以只需要看合并那条铁路要进的下一辆火车序 号跟第一条的一样和第二条的一样就行了,所以就有如下递归

if(tt==n+m){ok=1; return;}
if(t1<n && a[t1]==c[tt])f(t1+1, t2, tt+1);
if(t2<m && b[t2]==c[tt])f(t1,t2+1,tt+1);

最后看看ok 是不是等于 1 ,如果是,表示 possible ,不是表示 not possible ,但是悲催的这样 tle ……
这里需要一个对于俺来说已经好久没接触的东西:记忆化搜索。
p[i][j] 是第一条铁路进了 i 辆,第二条铁路进了 j 辆火车是否搜索过,如果已经搜索过了,那么就置 1 ,否则置 0. 然后如果搜索过了,直接 return 就行了

为神马要这个东西捏?
假设有这两条铁路
1 1 2 3
1 2 3 1
最后要合成的铁路是1 1 1 3 2 3 1 2 (显然不成立吧
那么很显然,调用函数的顺序是这个样子的:
f(0,0,0)->f(1,0,1)->f(2,0,2)-> f(2,1,3)
(没满足了呃…… ->f(2,0,2)->f(1,0,1)->f(1,1,2)-> f(2,1,3)
(呃…… 又来了 ……
所以说,要设置一个记忆化搜索的数组p ,这样就能过了 ……
最后附上代码……


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值