题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理。 需要考虑到各种情况, 分类处理。

常见错误:使用前未清空栈

使用STL思路较为清晰

代码附上, 欢迎各位大神指点~~

#include <cstdio>
#include <stack>
#include <iostream>
#include <vector>
using namespace std;
stack<char> s;
const int maxn = + ;
char in[maxn], out[maxn]; //记录栈的原始次序, 出栈次序
vector<string> str; //用以记录元素出入过程
int solve(int n)
{
int A = , B = ;
while(B < n){
if(in[A] == out[B]){
s.push(in[A++]);
str.push_back("in");
s.pop(); B++;
str.push_back("out");
}
else if(!s.empty()&&s.top() == out[B]){
s.pop();
str.push_back("out");
B++;
}
else if(A < n){
s.push(in[A++]);
str.push_back("in");
}
else return ;
}
if(s.empty()) return ;
else return ;
} void print()
{
for(vector<string>::iterator i = str.begin(); i != str.end(); i++){
cout << *i << endl;
}
} int main()
{
int n;
while(~scanf("%d", &n)){
getchar();
str.clear();
memset(in, , sizeof(in));
memset(out, , sizeof(out));
while(!s.empty()) s.pop();
scanf("%s%s", in, out);
if(solve(n)){
printf("Yes.\n");
print();
printf("FINISH\n");
}
else{
printf("No.\n");
printf("FINISH\n");
}
}
return ;
}

HDU1022 Train Problem I 栈的模拟的更多相关文章

  1. Train Problem I(栈)

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

  2. train problem I (栈水题)

    杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...

  3. Hdu 1022 Train Problem I 栈

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

  4. hdu1022 Train Problem I---模拟栈

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意: 车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若 ...

  5. Train Problem(栈的应用)

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

  6. hdu Train Problem I(栈的简单应用)

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

  7. hdu1022 Train Problem I

    http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...

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

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

  9. HDU Train Problem I 1022 栈模拟

    题目大意: 给你一个n 代表有n列 火车,  第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序, 分析一下就知道这,这个问题就是栈, 先进后出吗, ...

随机推荐

  1. ztree使用系列四(ztree实现同级拖拽排序并将排序结果保存数据库)

    ztree这个系列的最后一篇,也是ztree功能强大的体现之中的一个--排序功能. ztree能够实现全部节点之间任意的拖拽排序功能.我这里依据须要实现了仅仅同意同级之间任意拖拽排序,事实上原理都一样 ...

  2. 关于Autofac的使用陷阱

    说明:本文是对参考文章的一个整理,做一个记录,基本没有翻译,详情请参看原文. 使用注意要点: Autofac will track every disposable component instanc ...

  3. errno多线程安全(转载)

    一.errno的由来     在C编程中,errno是个不可缺少的变量,特别是在网络编程中.如果你没有用过errno,那只能说明你的程序不够健壮.当然,如果你是WIN32平台的GetLastError ...

  4. 错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”的解决方法

    编写布局代码时发现xml脚本出现错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”,原来是一个na ...

  5. linux centos各种问题解决汇总

    1. Linux终端启动图形化程序界面时报错: No protocol specified问题: 这是因为Xserver默认情况下不允许别的用户的图形程序的图形显示在当前屏幕上. 如果需要别的用户的图 ...

  6. Preventing CSRF in Java web apps---reference

    reference from:http://ricardozuasti.com/2012/preventing-csrf-in-java-web-apps/ Cross-site request fo ...

  7. 如何编写自己的Linux安全检查脚本?

    因为本人工作中要涉及到很多东西,审计(日志.数据神马的).源代码审计.渗透测试.开发一大堆东西,有些东西,越是深入去做,越会发现,没有工具或脚本,工作起来是有多么的坑. 工作的这段时间,自己写了几个工 ...

  8. Objective-C学习笔记

    . 链接:Objective-C学习笔记 (pdf版) .

  9. 琐碎-hadoop2.2.0-hbase0.96.0-hive0.13.1整合

    关于hadoop和hive.hbase的整合就不说了,这里就是在hadoop2.2.0的环境下整合hbase和hive 因为hive0.12不支持hadoop2,所以还要替换一些hadoop的jar包 ...

  10. Wcf 之 配置文件解析

    在WCF Service Configuration Editor的使用中,我们通过配置工具自动生成了WCF服务端的config文件.现在我们来看下这个配置文件各个标签的意义(解释在下面xml文件中的 ...