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

    Description 郭橐驼,不知始何名.病偻,隆然伏行,有类橐驼者,故乡人号之驼.驼闻之,曰:“甚善.名我固当.”因舍其名,亦自谓橐驼云.其乡曰丰乐乡,在长安西.驼业种树,凡长安豪富人为观游及卖果 ...

  2. [HDOJ5791]Two(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5791 题意:给两个数列,求有多少个公共子序列. dp(i,j)表示a1~ai和b1~bj的公共子序列个 ...

  3. [HDOJ1232]畅通工程

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. [SAP ABAP开发技术总结]ALV

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. SQL Server小技巧【1】

    1.SQL防止修改数据时引起多用户并发,当一条数据被一个用户锁定的时候其他用户将无法修改,除非将其释放. UPDATE TABLENAME WITH(ROWLOCK) SET 字段='Value' W ...

  6. CUBRID学习笔记 40 使用net修改数据

    修改 connection.Open();     string queryString = "UPDATE nation set capital = 'X' where `code` = ...

  7. CUBRID学习笔记 38 net调用java的函数过程

    首先要设置java_stored_procedure 为yes  该配置项在cubrid.conf中 书写并编译java代码 public class SpCubrid{      public st ...

  8. 打开Domion 提示: 管理员ID过期

    今天打开Domion 提示 管理员ID过期,什么操作都做不了,如是在网上趴了下,发现以下方法好用: 管理员ID文件被设置为允许超期,同时又没有其他ID文件可以用于访问服务器.如果尝试用已经超期的管理员 ...

  9. jQuery的.click,.bind,.unbind,.on,.off,.delegate,.undelegate

    .click与.bind .click和.bind都是给每个元素绑定事件,对于只绑定一个click事件,.bind事件的简写就是.click那种方式. 这两种方式都会出现两个问题: 第一个问题,如果要 ...

  10. php工具 phpstorm 的快捷键 的使用(待添加

    参考网址:http://www.cnblogs.com/jikey/p/3491798.html 1. ctrl+tab 键,可以切换各个选项卡 页面 2. shift+enter 键,无论光标在本行 ...