hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
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



3 123 312
in
in
in
out
out
out
FINISH
No.
FINISH
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.".
#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)的更多相关文章
- HDU 1022 Train Problem I(栈的操作规则)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...
- 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 ...
- hdu 1022 Train Problem I(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- HDOJ/HDU 1022 Train Problem I(模拟栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- hdu 1022 Train Problem I(栈)
#include<iostream> #include<vector> #include<cstring> #include<string> #incl ...
- HDU 1022 Train Problem I 用栈瞎搞
题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...
- HDU 1022 Train Problem I 模拟栈题解
火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...
- hdu 1022 Train Problem I【模拟出入栈】
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- UG NX9.0.0 for linux安装
自:http://vivianyw.blog.163.com/blog/static/13454742220145601415881/?latestBlog NX9.0.0 for linux安装. ...
- ZooKeeper安装方法具体解释
ZooKeeper安装方式分为两种,一种为单机模式.一个为集群模式,集群模式须要事先正确配置hadoop集群,安装方法參考hadoop-1.2.1安装方法具体解释 单机模式安装: 1.上传并解压zoo ...
- <续>调度算法补充
cpmpute->executors: 1.从storm配置获取<compoent-id,parallelism>集合 2.storm-task-info 获得<task-id ...
- RedHat Enterprise Linux 6 配置Xmanager ,实现图形界面连接
我们经常见到的几种最为常用的windows下远程管理Linux服务器的方法,基本上都是利用SecureCRT,或者是PUTTY等客户端工具通过ssh服务来实现Windows下管理Linux服务器的,这 ...
- 初学者必知的Python中优雅的用法
转自:http://python.jobbole.com/81393/
- Android API之android.os.AsyncTask
android.os.AsyncTask<Params, Progress, Result> AsyncTask enables proper and easy use of the UI ...
- db.students.batchInsert is not a function :@(shell):1:1
按照<mongdb权威指南>当使用version 3.4.1版本的mongodb,其中使用batchInsert函数进行对students集合进行批量插入 db.students.batc ...
- SCF: 简单配置门面
SCF: 简单配置门面 [English] [中文] Simple Configuration Facade, 简写为 SCF.是 代码 和 外部配置 (properties文件, 环境变量,系统/ ...
- Python --标准库 存储对象 (pickle包,cPickle包)
在之前对Python对象的介绍中 (面向对象的基本概念,面向对象的进一步拓展),我提到过Python“一切皆对象”的哲学,在Python中,无论是变量还是函数,都是一个对象.当Python运行时,对象 ...
- Unity3D安卓交互 - 使代码运行在UI线程
runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub } });