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 ...
随机推荐
- Unix 网络编程 I/O 模型 第六章
前提,也是重点是, 当接收收据.或者读取数据时,分两步 1 等待数据准备好. 2 从内核拷贝数据到进程. 对于一个network IO 即 socket(这里我们以read举例),它会涉及到两个系统对 ...
- Dao泛型设计和反射反型
(1)DAO泛型设计:当二哥或多个类中有类似的方法时,可以将这些累死的方法提出到类中,形式一个泛型父类 (2)反射反型:在泛型父类中获取子类的具体类型的过程,叫反射反型 package cn.itca ...
- 利用DM工具Weka进行数据挖掘(分类)的完整过程
利用DM工具Weka进行数据挖掘(分类)的完整过程:
- mysql(表类型的选择)
1.查询mysql所支持的存储引擎 第一种方法:show engines \G
- FFmpeg在Linux下编译使用
文章转自:http://www.cnblogs.com/CoderTian/p/6655568.html 1.FFmpeg编译 1.1.安装yasm 这里我是直接通过ubuntu包安装的,当然也可以通 ...
- Redis总结(一)Redis安装(转载)
最近项目中需要使用Redis,刚好这两天有时间,便总结记录一下Redis的安装,以及如何在.NET中使用Redis. Redis是一个用的比较广泛的Key/Value的内存数据库.目前新浪微博.Git ...
- EMQ ---客户端clientid为空,emq会随机帮忙生成
mqtt v3.1.1协议有规定clientid可以为空,所以当客户端clientid为空,emq会随机帮忙生成. 如果clientid为空,随机生成clientid.例如'emqttd_105789 ...
- exception in initAndListen: 12596 old lock file, terminating
#mongd -f /etc/mongodb.conf时报错 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamFjc29uX2JhaQ==/font/5a ...
- unity, GUI.Button texture is black
GUI.Button(rect,tex),结果显示出来tex是黑的,原来是因为我以前在别处调用了GUI.contentColor =Color.black. 参考:http://answers.uni ...
- Informix 配置选项
下表列出了用于改善数据库性能的附加选项.如果选择在 onconfig 配置文件中进行更改,则需要重新启动 Informix 通用服务器.可以在 Informix 服务器处于联机或脱机状态时编辑 onc ...