题目链接: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. cocos2d-html5 笔记5: 事件

    在cocos2d里面,通过Node的方式,将整个场景以及里面的object给组织起来,这样很容易画了,从root node开始遍历,把整棵树画出来就是了. 剩下就是animation,timer, 还 ...

  2. mysql 参数read_rnd_buffer_size的真正含义

     http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html  http://dev.mysql.com/doc/refman/ ...

  3. MySQL · 引擎特性 · InnoDB 事务锁简介

    https://yq.aliyun.com/articles/4270# zhaiwx_yinfeng 2016-02-02 19:00:43 浏览2194 评论0 mysql innodb lock ...

  4. cisco路由基于策略的路由选择

    cisco路由基于策略的路由选择 基于策略的路由选择是一种手段,通过它管理员可以在基于目的地的路由选择协议中实现偏离标准路由的路由选择.基于目的地的路由选择协议将根据到一个目的地的最短路径选择路由,基 ...

  5. Fedora 23如何安装LAMP服务器

    LAMP 是开源系统上 Web 服务器的梦幻组合.LAMP 是 Linux. Apache HTTP 服务. MySQL/MariaDB 数据库和 PHP. Perl 或 Python 的简称. 下面 ...

  6. java中如何忽略字符串中的转义字符--转载

    原文地址:http://my.oschina.net/u/1010578/blog/366252 起因     这几天工作上需要跟另一个同事联调rest接口,我这边是java他是php,返回报文是js ...

  7. 分布式缓存技术redis学习(二)——详细讲解redis数据结构(内存模型)以及常用命令

    Redis数据类型 与Memcached仅支持简单的key-value结构的数据记录不同,Redis支持的数据类型要丰富得多,常用的数据类型主要有五种:String.List.Hash.Set和Sor ...

  8. JS后退, JS返回上一页, JS返回下一页

    Javascript 返回上一页: 1. history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). 3. window.history.fo ...

  9. iOS 中的XML解析代码(SAX)

    1.XML解析(SAX) NSXMLParser SAX 大文件 1)打开文档 - (void)parserDidStartDocument:(NSXMLParser *)parser 2)开始查找起 ...

  10. Visual Studio 2015 与GitLab 团队项目与管理【2】

    前一篇介绍了Git服务器的搭建,我采用的是CentOS7-64位系统,git版本管理使用的是GitLab,创建管理员密码后进入页面. 创建Users,需要记住Username和邮箱,初始密码可以由管理 ...