Railroad

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

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

 
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
 
题目的意思:  给你两个数组a,b  让a,b两个数组按其原序进行组合,问能否组合成为c数组。
 
我们可以试着用搜索的方式进行处理,但是由于数据较大,而且在处理的过程中,有重叠的状态,所以我们需要用到记忆话,对于原先有的状态之后的搜索,我们不去在重复,这样就节省了很多的时间。
代码:
 //#define LOCAL
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
int aa[maxn],bb[maxn],cc[maxn<<];
int mat[maxn][maxn];
int n1,n2;
bool flag;
void dfs(int lena,int lenb,int lenc)
{
if(lena==n1+&&lenb==n2+){
flag=;
return ;
}
if(mat[lena][lenb]) return ;
if(!flag&&aa[lena]==cc[lenc]){
mat[lena][lenb]=;
dfs(lena+,lenb,lenc+);
}
if(!flag&&bb[lenb]==cc[lenc]){
mat[lena][lenb]=;
dfs(lena,lenb+,lenc+);
}
}
void input(int n,int a[])
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
while(scanf("%d%d",&n1,&n2)&&n1+n2!=)
{
input(n1,aa);
aa[n1+]=-;
input(n2,bb);
bb[n2+]=-;
input(n1+n2,cc);
flag=;
memset(mat,,sizeof(mat));
dfs(,,);
if(flag)printf("possible\n");
else printf("not possible\n");
}
return ;
}

hdu---(3779)Railroad(记忆化搜索/dfs)的更多相关文章

  1. 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof

    题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...

  2. 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

    题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...

  3. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  4. hdu1978How many ways (记忆化搜索+DFS)

    Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标 ...

  5. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs

    题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...

  6. hdu 4826(dp + 记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...

  7. POJ1088滑雪(记忆化搜索+DFS||经典的动态规划)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 84297   Accepted: 31558 Description M ...

  8. hdu 4345 Permutation 记忆化搜索

    思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...

  9. POJ 1198 / HDU 1401 Solitaire (记忆化搜索+meet in middle)

    题目大意:给你一个8*8的棋盘,上面有四个棋子,给你一个初始排布,一个目标排布,每次移动,可以把一个棋子移动到一个相邻的空位,或者跨过1个相邻的棋子,在保证棋子移动不超过8次的情况下,问能否把棋盘上的 ...

随机推荐

  1. WEB前端开发工具的初识

    准备学习BootStrap,然后发现好多插件啊…… 从一个开源项目开始学习吧. required node.js & bower & grunt $ git clone https:/ ...

  2. V-rep学习笔记:机器人逆运动学数值解法(The Pseudo Inverse Method)

    There are two ways of using the Jacobian matrix to solve kinematics. One is to use the transpose of ...

  3. CUBRID学习笔记 16 元数据支持

    简化了很多 ,在sqlserver需要用语句实现的功能 接口如下 public DataTable GetDatabases(string[] filters) public DataTable Ge ...

  4. FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)

    FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术) Time Limit: 1000MS   Memory Limit: 257792K [Description ...

  5. git学习笔记12-标签管理-版本

    发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照 ...

  6. ubuntu12.04 gdb安装使用

    参考文档:http://blog.csdn.net/haoel/article/details/2879 http://www.programlife.net/gdb-manual.html [新手笔 ...

  7. PL/SQL 听课笔记

    PL/SQL: 知识回顾: SQL:  结构化查询语言: T-SQL:  microsoft sql语言: PL/SQL: Oracle sql语言: 变量命名规则: 1.首字母必须是字母,可以包含字 ...

  8. Android获取窗体信息的Util方法

    package com.wangyi.tools; import android.app.Activity; import android.util.DisplayMetrics; public cl ...

  9. iOS - UIButton

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIButton : UIControl <NSCoding> @available(iOS 2.0, ...

  10. PHP基础知识之————PDO预处理语句

    转载处:http://www.cnblogs.com/xiaohuochai/p/6133353.html 定义 在生成网页时,许多PHP脚本通常都会执行除参数之外,其他部分完全相同的查询语句,针对这 ...