题目链接: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. php获取当前url完整地址

    //获取当前访问的完整url地址 function getCurUrl() { $url = 'http://'; if (isset($_SERVER['HTTPS']) && $_ ...

  2. Win7 IE故障:APPCRASH,d3d9.dll,c0000005

    问题:        今天使用使用IE登录某网址,发现总是报错,如下图,无法浏览.         解决方案:       主要讲IE的呈现方案修改即可,如下步骤:       在IE的[Intern ...

  3. shell 学习笔记

    <Linux命令行与shell脚本编程大全>笔记   wkss 其他:http://www.cnblogs.com/pengdonglin137/p/3528303.html 一.基本命令 ...

  4. oracle数据库常用SQL语句

    1)删除表的一列 ALTER TABLE 表名 DROP COLUMN 列名; 2)增加表的一列 且默认值为0 alter table 表名 add 字段名 类型 default '0'; 3)修改表 ...

  5. recent.css常用的页面初始化样式

    <style> @charset "utf-8"; body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form ...

  6. WPF省市联动Binding

    主要思路: 把省的ItemsSource绑定DataContext,然后给市的ItemsSource绑定到Element(省)的SelectedItem上 xaml <Window x:Clas ...

  7. hdu-5701 中位数计数(中位数)

    题目链接: 中位数计数 Problem Description   中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均数作为中位数. 现在有nn个数,每个 ...

  8. GSS6 4487. Can you answer these queries VI splay

    GSS6 Can you answer these queries VI 给出一个数列,有以下四种操作: I x y: 在位置x插入y.D x  : 删除位置x上的元素.R x y: 把位置x用y取替 ...

  9. MarkDown认识与入门

    Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...

  10. JavaScript--匿名函数和闭包(16)

    // 匿名函数:没有名字的函数; // 闭包:可访问一个函数作用域里的变量的函数; 一 匿名函数 // 普通函数 function box(){ // 函数名是box; return 'Lee'; } ...