题目大意:

给你一个n 代表有n列 火车,  第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序,

分析一下就知道这,这个问题就是栈, 先进后出吗, 就是问你这个编号有没有可能出现, 有可能的话输出顺序,没可能直接输出No

题目分析:

我们用栈进行模拟, 先判断是否有这种出栈的可能, 假如有这种可能的话我们在模拟 一遍出栈的顺就行了。

每一次有新元素入栈我们就判断他是否雨 第二个字符串的第一个元素是否相等, 假如一样, 就让次元素出栈。

代码

  1. #include <iostream>
  2. #include <queue>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <stack>
  7. using namespace std;
  8. #define maxn 15
  9.  
  10. bool OK(char str1[], char str2[],int n)//判断是否存在这种出栈的可能
  11. {
  12. stack<char> P;
  13. P.push('#');//因为有元素要删除赋值, 不能为空, 我们让第一个元素为‘#’
  14. char ans;
  15. int i, j;
  16. for(i=, j=; i<n; i++)
  17. {
  18. P.push(str1[i]);
  19. ans = P.top();
  20. while(ans == str2[j] )
  21. {
  22. P.pop();
  23. j ++;
  24. ans = P.top();//此处是要赋值判断的, 假如为空的话就无法赋值, 因此我们让第一个元素为‘#’
  25. }
  26. }
  27. if(j == n)
  28. return true;
  29. return false;
  30. }
  31.  
  32. void Putt(char str1[], char str2[],int n)
  33. {
  34. stack<char> P;
  35. P.push('#');
  36. for(int i=, j=; i<n; i++)
  37. {
  38. P.push(str1[i]);
  39. printf("in\n");
  40. char ans = P.top();
  41. while(ans == str2[j])
  42. {
  43. P.pop();
  44. printf("out\n");
  45. j ++;
  46. ans = P.top();
  47. }
  48. }
  49. }
  50. int main()
  51. {
  52. int n;
  53. char str1[maxn], str2[maxn];
  54. while(cin >> n >> str1 >> str2)
  55. {
  56. if(OK(str1, str2, n) )
  57. {
  58. printf("Yes.\n");
  59. Putt(str1,str2,n);
  60. }
  61. else
  62. printf("No.\n");
  63.  
  64. printf("FINISH\n");
  65. }
  66. return ;
  67. }

HDU Train Problem I 1022 栈模拟的更多相关文章

  1. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  2. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  3. hdu 1022 Train Problem I(栈的应用+STL)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  5. Train Problem I--hdu1022(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. hdu Train Problem I

    这道题是道简单的栈模拟题,只要按照真实情况用栈进行模拟即可: #include<stdio.h> #include<string.h> #include<stack> ...

  7. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

  8. HDU 1022 Train Problem I 用栈瞎搞

    题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...

  9. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

随机推荐

  1. Linux如何关闭防火墙和查看防火墙的具体情况

    1.Linux下关闭和开启防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: ser ...

  2. When does layoutSubviews get called?

    转自:http://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/ It’s important to optim ...

  3. NSDateFormatter 格式说明

    格式化参数如下:    G: 公元时代,例如AD公元    yy: 年的后2位    yyyy: 完整年    MM: 月,显示为1-12    MMM: 月,显示为英文月份简写,如 Jan    M ...

  4. java多态---内存关系

    在该列中,a.lookDoor()会报错,因为azhong没有lookDoor这个方法,同理,a.playGame()也会报错. 注意!!! 最后一句Dog dd=(Dog)a: 这句话非常错误! 在 ...

  5. 【IOS】 XML解析和xml转plist文件(GDataXML)

    iOS对于XML的解析有系统自带的SDK--NSXMLParser,鄙人愚拙,只会用GDataXML进行解析,这里仅介绍GData的使用.(以下图为例) 1.对于一个xml文件,先读取出来 NSDat ...

  6. 地址栏访问Action,后来方法执行两次

    SSH框架,在地址栏输入URL访问Action,后台访问会访问两次.很奇怪. 经排查,最终问题在于方法名称写错了.将getOpinionByPN()修改成queryOpinionByPN(),没有问题 ...

  7. c#将Excel数据导入到数据库的实现代码(OleDb)

    sing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web ...

  8. 多个显示器, window.open的定位

    // Pops a window relative to the current window position function popup(url, winName, xOffset, yOffs ...

  9. C# 判断字符串是否可以转化为数字

    C# 判断字符串是否可以转化为数字 /// <SUMMARY> /// 判断字符串是否可以转化为数字 /// </SUMMARY> /// <PARAM name=&qu ...

  10. Subversion 1.7 Eclipse integration in Ubuntu12(转载)

    原文链接:http://steveliles.github.io/subversion_1_7_eclipse_integration_in_ubuntu.html Getting Subversio ...