最长上升子序列1004
来源:互联网 发布:python 股票数据库 编辑:程序博客网 时间:2024/06/07 04:08
Railroad
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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+N2 positive integers giving the desired order for the departing train (same format as above).
The end of input is indicated by N1 = N2 = 0.
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 31 2 12 1 11 2 1 1 2 13 31 2 12 1 21 1 1 2 2 20 0
Sample Output
possiblenot possible
思路:这题就是将来两个序列组合起来看是否能组成第三个序列,一开始用dfs,竟然wa了,然后用dp写就过了。
代码:
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;int a[1005],b[1005],c[2005];int dp[1005][1005];int main(){ int m,n; while(cin>>m>>n) { if(m==0&&n==0)break; int i,j; for(i=1;i<=m;i++) { scanf("%d",&a[i]); } for(i=1;i<=n;i++) { scanf("%d",&b[i]); } for(i=1;i<=m+n;i++) { scanf("%d",&c[i]); } memset(dp,0,sizeof(dp)); dp[0][0]=1; for(i=0;i<=m;i++) { for(j=0;j<=n;j++) { if(i>0&&c[i+j]==a[i]&&dp[i-1][j]) { dp[i][j]=1; } if(j>0&&c[i+j]==b[j]&&dp[i][j-1]) { dp[i][j]=1; } } } if(dp[m][n])cout<<"possible"<<endl; else cout<<"not possible"<<endl; } return 0;}
阅读全文
0 0
- 最长上升子序列1004
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- java add Library
- PHP session有效期session.gc_maxlifetime
- 多种方式找出PHP配置文件php.ini的路径总结
- 剑指offer 47----实现一个Add函数,让两个数相加,但是不能使用+、-、*、/等四则运算符。ps:也不能用++、--
- 洛谷 P3338 [ZJOI2014]力(bzoj P3527 [ZJOI2014]力)
- 最长上升子序列1004
- Mockplus推出真正无限制终身版,做原型就要一辈子!
- Mac下安装mysql5.7 完整步骤(图文详解)
- RMQ模板(不保证正确)
- Mysql数据库CPU占用达到80%的简单排查解决办法
- C语言里的EOF(结束程序)怎么用?
- 微信小程序新增功能: 多列模式和省市区类型
- 链表环的判断,是否相交
- unable to find SASL plugin: PLAIN kudu