HDU1022 Train Problem I 栈的模拟
题目链接: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 栈的模拟的更多相关文章
- Train Problem I(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- train problem I (栈水题)
杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1022 Train Problem I---模拟栈
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意: 车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若 ...
- Train Problem(栈的应用)
Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- hdu1022 Train Problem I
http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...
- hdu 1022 Train Problem I【模拟出入栈】
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU Train Problem I 1022 栈模拟
题目大意: 给你一个n 代表有n列 火车, 第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序, 分析一下就知道这,这个问题就是栈, 先进后出吗, ...
随机推荐
- MySQL 5.7 深度解析: JSON数据类型使用
http://www.actionsky.com/docs/archives/156 2015年12月25日 杨涛涛 JSON (JavaScriptObject Notation) 是一种轻量级 ...
- 获取div相对文档的位置
获取div相对文档的位置,两个方法 经测试 document.getElementById("btn").getBoundingClientRect() 在IE6下有2像素的bug ...
- 计划任务命令crontab、at
一.为计划任务提供支持 开始为系统建立计划任务之前,需要为系统添加相关设置,以确保计划任务能够正确运行.计划任务需要的支持主要包括两个方面:正确运行系统服务.准确的系统时间. 1.正确运行系统服务 为 ...
- linux上一些命令
ps -ef看看有没有tomcat的进程:也可以用netstat -tnl来看tomcat的端口是否开放
- iOS 10 推送必看(高阶1)
来源:徐不同 链接:http://www.jianshu.com/p/3d602a60ca4f iOS10 推送必看(基础篇) 虽然这篇文章比较长,也不好理解,但是还是建议大家收藏,以后用到的时候,可 ...
- java 转义字符
\t 相当于tab,缩进\n 回车\r 换行\b 换成 一个黑点
- [C.Sharp] TimeSpan的用法,获取测试程序运行时间
TimeSpan的用法 TimeSpan是用来表示一个时间段的实例,两个时间的差可以构成一个TimeSpan实例,现在就来简单介绍一下几点重要的用法: a 先来介绍几个方法 TimeSpan.Minu ...
- PHP 错误处理机制
在日常的项目开发过程中,总是会出现一些我们意想不到的异常错误,如果我们对此没有进行相对完善的处理,那么程序看上去也很不专业,也很可能就会成为别人攻击系统的有效信息:有些错误异常会终止脚本执行,这个时候 ...
- Linux下cut命令使用
标题:cut命令的使用 作用:cut命令是对文件以行为单位,按照字节.字符.指定的域分隔符对行进行剪切,提取所需要的片段内容. 一.用法: cut [-bn] [file] 或cut -c [file ...
- 程序编码(机器级代码+汇编代码+C代码+反汇编)
[-1]相关声明 本文总结于csapp: 了解详情,或有兴趣,建议看原版书籍: [0]程序编码 GCC调用了一系列程序,将源代码转化成可执行代码的流程如下: (1)C预处理器扩展源代码,插入所有用#i ...