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列 火车, 第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序, 分析一下就知道这,这个问题就是栈, 先进后出吗, ...
随机推荐
- Web service是什么?(转)
我认为,下一代互联网软件将建立在Web service(也就是"云")的基础上. 我把学习笔记和学习心得,放到网志上,欢迎指正. 今天先写一个最基本的问题,Web service到 ...
- C/C++基础总结
1 static(静态)变量有什么作用 3个体明显的作用:1)在函数体内,静态变量具有“记忆”功能,即一个被声明为静态变量在一个函数被调用的过程中其值维持不变2)在模块内,它的作用域范围是有限制的,即 ...
- Linux下杀僵尸进程办法
1) 检查当前僵尸进程信息 # ps -ef | grep defunct | grep -v grep | wc -l 175 # top | head -2 top - 15:05:54 up 9 ...
- 为VS2010默认模板添加版权信息 .
通过以下方式可以自定义CS类文件代码模板(以下为VS2010,VS2008类似): 1,打开VS的安装目录,例如 D:\Program Files\Microsoft Visual Studio 10 ...
- Ubuntu目录
1. java.io.FileNotFoundException: ***(Too many open files) 2. 在Ubuntu 12.04 桌面上设置启动器(快捷方式) 3. 解决Ubun ...
- BootStrap2学习日记15----选项卡
导航格式1: <ul class="nav nav-tabs"> <li class="active"><a href=" ...
- 2.1.6 用ProtectX实现扫描的反击与追踪
ProtectX是一款在用户连接网络时保护电脑的工具,可以同时监视20个端口,还可以帮助追踪攻击者的来源.一旦有人尝试连接到用户的电脑,它即可发出声音警告并将入侵者的IP位址记录下来,可以防止黑客入侵 ...
- 3. Android框架和工具之 xUtils(BitmapUtils)
1. BitmapUtils 作用: 加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象: 支持加载网络图片和本地图片: 内存管理使用 ...
- Android中Handler作用
在Android的UI开发中,我们经常会使用Handler来控制主UI程序的界面变化.有关Handler的作用,我们总结为:与其他线程协同工作,接收其他线程的消息并通过接收到的消息更新主UI线程的内容 ...
- 将Winform程序快速转换为在浏览器中运行的程序
http://www.codeproject.com/Articles/31429/Embedding-a-NET-WinForms-Application-in-an-Interne 详见以上文章. ...