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

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

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

使用STL思路较为清晰

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

  1. #include <cstdio>
  2. #include <stack>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6. stack<char> s;
  7. const int maxn = + ;
  8. char in[maxn], out[maxn]; //记录栈的原始次序, 出栈次序
  9. vector<string> str; //用以记录元素出入过程
  10. int solve(int n)
  11. {
  12. int A = , B = ;
  13. while(B < n){
  14. if(in[A] == out[B]){
  15. s.push(in[A++]);
  16. str.push_back("in");
  17. s.pop(); B++;
  18. str.push_back("out");
  19. }
  20. else if(!s.empty()&&s.top() == out[B]){
  21. s.pop();
  22. str.push_back("out");
  23. B++;
  24. }
  25. else if(A < n){
  26. s.push(in[A++]);
  27. str.push_back("in");
  28. }
  29. else return ;
  30. }
  31. if(s.empty()) return ;
  32. else return ;
  33. }
  34.  
  35. void print()
  36. {
  37. for(vector<string>::iterator i = str.begin(); i != str.end(); i++){
  38. cout << *i << endl;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. int n;
  45. while(~scanf("%d", &n)){
  46. getchar();
  47. str.clear();
  48. memset(in, , sizeof(in));
  49. memset(out, , sizeof(out));
  50. while(!s.empty()) s.pop();
  51. scanf("%s%s", in, out);
  52. if(solve(n)){
  53. printf("Yes.\n");
  54. print();
  55. printf("FINISH\n");
  56. }
  57. else{
  58. printf("No.\n");
  59. printf("FINISH\n");
  60. }
  61. }
  62. return ;
  63. }

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. 在Swift中的ASCII到字符转换的问题

    我们在C++里处理字符通常是这样的 char a = 'A' // A = 65 printf("'%c' = %d", a + 1, a + 1) // 'B' = 66 这在号 ...

  2. C++11 之for 新解

     前言     C++11这次的更新带来了令非常多C++程序猿期待已久的for range循环,每次看到javascript. lua里的for range.心想要是C++能有多好,心里别提多酸了.这 ...

  3. 商户怎样选择商业wifi进行移动营销

    互联网移动营销的大潮席卷而来,带给很多其它的商户营销理念上的升级和更新.商业wifi营销成为很多其它的商户选择.以往,人们在咖啡店不过聊聊天喝喝咖啡,如今,人们能够更悠闲的歇息娱乐享受带有wifi的咖 ...

  4. python的print(转)

    转载:http://www.pythonclub.org/python-basic/print   使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello = 'Hello ...

  5. Matlab程序怎样打包

    本人安装的版本号是MATLAB(R2010b) 打包过程例如以下: MATLAB命令窗体输入deploytool,打开一个Eeployment Project的窗体: 1.在Name输入你想要打包后的 ...

  6. poj 2688 状态压缩dp解tsp

    题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...

  7. cocos2d-x中CCTextureCache图片资源的异步加载

    如果没有预先加载图片,则可以通过addImageAsync()函数实现异步加载,该函数通过创建一个加载线程来加载图片,并且在主线程中通过调用回调函数来读取该图片资源纹理.其主要过程如下: 1.创建线程 ...

  8. MapReduce原理及其主要实现平台分析

    原文:http://www.infotech.ac.cn/article/2012/1003-3513-28-2-60.html MapReduce原理及其主要实现平台分析 亢丽芸, 王效岳, 白如江 ...

  9. SIFT算法的应用--目标识别之Bag-of-words模型

    原文:http://blog.csdn.net/v_JULY_v/article/details/6555899 SIFT算法的应用 -目标识别之用Bag-of-words模型表示一幅图像 作者:wa ...

  10. 基于Docker服务的java Web服务搭建

    导读 最近想我们的应用需要更新维护,Android.IOS.还有服务器端都要更新,都在忙于写代码没有写文章了.我们的服务器是用java ssh架构的,到时也打算切换成Spring MVC+oauth2 ...