题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程。

题目思路:栈的特点是先进后出,和题意类似,还有有一种情况是:开进来立马有开出去。并用vis[]数组的0,1标记进出站情况。

具体看代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<queue>
  5. #include<algorithm>
  6. #include<iostream>
  7. #define MAX 100005
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. int A,B,top,i,j,n,vis[MAX],stack[MAX],ok;
  13. char str1[MAX],str2[MAX];
  14. while(scanf("%d%s%s",&n,str1,str2)!=EOF)
  15. {
  16. ok=;
  17. memset(vis,,sizeof(vis));
  18. i=;
  19. A=;//当前str1的位置
  20. B=;//当前str2的位置
  21. top=;//栈顶
  22. while(B<n)
  23. {
  24. if(str2[B]==str1[A])//如果两者相同就是即进即出的情况
  25. {
  26. vis[i++]=;//进站
  27. vis[i++]=;//出站
  28. A++;
  29. B++;
  30. }
  31.  
  32. else if(top && stack[top]==str2[B])//如果栈非空,而且栈顶元素=str2当前元素则弹出栈
  33. {
  34. vis[i++]=;
  35. top--;
  36. B++;
  37. }
  38.  
  39. else if(A <= n)
  40. {
  41. stack[++top]=str1[A++];//压入栈
  42. vis[i++]=;
  43. }
  44.  
  45. else
  46. {
  47. ok=;
  48. break;
  49. }
  50. }
  51.  
  52. if(!ok)
  53. {
  54. printf("No.\nFINISH\n");
  55. }
  56.  
  57. else
  58. {
  59. printf("Yes.\n");
  60. for(j=;j<i;j++)
  61. {
  62. if(vis[j])
  63. printf("in\n");
  64. else
  65. printf("out\n");
  66. }
  67. printf("FINISH\n");
  68. }
  69. }
  70. return ;
  71. }

HDU 1022 Train Problem I 用栈瞎搞的更多相关文章

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

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

  2. 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 ...

  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(栈模拟)

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

  5. HDOJ/HDU 1022 Train Problem I(模拟栈)

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

  6. hdu 1022 Train Problem I(栈)

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

  7. HDU 1022 Train Problem I 模拟栈题解

    火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...

  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 1022 Train Problem I 栈

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

随机推荐

  1. POJ 2313 Sequence#贪心

    (- ̄▽ ̄)-* 找规律 //初始化为B[i]=A[i] //然后由V=|A[1]-B[1]|+|A[2]-B[2|+|A[3]-B[3]| // +|B[1]-B[2]|+|B[2]-B[3]| / ...

  2. 浙大pat 1031题解

    1031. Hello World for U (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Giv ...

  3. asp.net正则表达式去除a标签

    if (drr["allow_a"].ToString() == "False") { cont = dr["news_Content"]. ...

  4. JVM 几个重要的参数

    <本文提供的设置仅仅是在高压力, 多CPU, 高内存环境下设置>  最近对JVM的参数重新看了下, 把应用的JVM参数调整了下.  几个重要的参数 -server -Xmx3g -Xms3 ...

  5. Objective-C中的instancetype与id的区别

    一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...

  6. maven入门(上)

    Apache Maven 入门篇 ( 上 ) 作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这 ...

  7. beginBackgroundTaskWithExpirationHandle

    [[UIApllication sharedApplication] beginBackgroundTaskWithExpirationHandle:^{}];这个方法在app进入后台时,可以做一些事 ...

  8. elicpse之tomcat配置

    环境:eclipse4.5.0,tomcat7.0.57 部署描述:按照一般的部署,把tomcat部署到webapps的下面,server options 下面选的是 Modules auto rel ...

  9. iOS中的触摸事件,手势识别,摇晃事件等

    在iOS中,事件可以划分为以下几类: 1.触摸事件:通过触摸,手势进行触发(手指点击.缩放等) 2.运动事件:通过加速器触发(例如手机晃动) 3.远程控制事件:通过其他远程设备触发(例如耳机控制按钮) ...

  10. tableView左滑按钮

    - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsFo ...