Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18095    Accepted Submission(s): 6764

Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
 
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
 
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 
Sample Input
3 123 321
3 123 312
 
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

Hint

Hint

For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".

 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  1023 1020 1032 1019 1012 

 
  数据结构:栈的使用 + 递归
  这道题上年的暑假做过,当时没做出来,这次用递归成功解决。
  思路用递归遍历所有情况并记录正确情况的步骤,最后输出。一开始没用递归,超时,我想是因为递归函数中剪枝做的比较好,所以反而要快。
  另外这道题用栈来做比较好。
  AC代码:
 
 #include <iostream>
#include <stack>
using namespace std;
char o1[],o2[];
bool q[];
int n;
bool dfs(bool c,int cn,int index1,int index2,stack <char> s)
{
if(cn>=*n)
return true;
if(c==true){ //当前要求入栈
if(index1>=n)
return false;
s.push(o1[index1++]);
q[cn] = c;
}
else {
if(s.empty())
return false; //当前这样操作不可以
else{
char t;
if(s.top()==o2[index2]){
index2++;
s.pop();
q[cn] = c;
}
else
return false;
}
}
if(dfs(true,cn+,index1,index2,s))
return true;
else if(dfs(false,cn+,index1,index2,s))
return true;
return false;
}
int main()
{
while(cin>>n){
cin>>o1>>o2;
//true为入栈,false为出栈
stack <char> s;
if(dfs(true,,,,s)){ //返回情况
cout<<"Yes."<<endl;
for(int i=;i<n*;i++)
if(q[i])
cout<<"in"<<endl;
else
cout<<"out"<<endl;
cout<<"FINISH"<<endl;
}
else{
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1022:Train Problem I(数据结构,栈,递归,dfs)的更多相关文章

  1. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  3. hdu 1022 Train Problem I(栈的应用+STL)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  5. HDOJ/HDU 1022 Train Problem I(模拟栈)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  6. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

  7. HDU 1022 Train Problem I 用栈瞎搞

    题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...

  8. HDU 1022 Train Problem I 模拟栈题解

    火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...

  9. hdu 1022 Train Problem I【模拟出入栈】

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. UML学习(一)-工具介绍

    这里用于学习UML的工具是StarUML,没有什么原因为什么要用它,或许仅仅是有人说好用和比较小. 首先介绍下这个工具,来张图. 1.菜单栏(最上面) 2.快捷工具栏(菜单栏下面) 3.工具项(Too ...

  2. Linux内核开发之异步通知与异步I/O(一)

    “小王,听说过锦上添花吧..”我拍拍下王的头说. “还锦上添花你,为你上次提的几个东东,我是头上长包..”小王气愤地瞪着我. “啊,为啥这样呢,本来还特意拒绝了MM的月份,抽出时间打算给你说点高级的东 ...

  3. centos下node.js的安装

    安装的路径我举例在home目录 1.cd /home 2.下载node.js最新版本 wget http://nodejs.org/dist/v0.10.28/node-v0.10.28.tar.gz ...

  4. Linux-软件包管理-yum在线管理-光盘yum源

    mount /dev/cdrom /mnt/cdrom 将设备名/dev/cdrom安装到mnt/cdrom挂载点下面mount 查看当前所有挂载信息 cd /etc/yum.repos.d 切换到e ...

  5. Cocos2d-x3.0 载入Cocostudio的UI后,button无法点击的解决方法

    原帖地址:http://blog.csdn.net/musicvs/article/details/28390617 近期发现不少朋友都遇到这个问题,用Cocostudio的UI编辑器创建好UI后,在 ...

  6. unity, GUI.Button texture is black

    GUI.Button(rect,tex),结果显示出来tex是黑的,原来是因为我以前在别处调用了GUI.contentColor =Color.black. 参考:http://answers.uni ...

  7. 蜜果私塾:informix数据库学习合集[不断补充]

    一.infomix使用备忘录     目录结构:     1. 启动与停止命令:      2. 修改数据库编码:      3. 查看informix占用的端口:      4. 使用dbacces ...

  8. C#设置文件夹用户权限

    var security = new DirectorySecurity();   string path=@"C:\temp" //设置权限的应用为文件夹本身.子文件夹及文件,所 ...

  9. HDU 1016:Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. Atitit.网页爬虫的架构总结

    Atitit.网页爬虫的架构总结 1. 总数的结构..(接口方法) 1 2. 获得页数 1 3. 跳页处理(接口方法) 2 4. 单个的页面处理(接口方法) 2 4.1. 获得页面url 3 4.2. ...