zoukankan      html  css  js  c++  java
  • hdu1022 Train Problem I

    本题是对栈和队列的综合运用。
    火车进站可以用栈来描述, 而进出栈的记录 in out 则用队列来记录 “1” 代表’in‘ , “0”代表“out”
    最后根据队列内容来输出in out
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <stack>
     4 #include <queue>
     5 
     6 
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     int n, a, b;
    12     char str1[20], str2[20];
    13     stack<char> sta;
    14     queue<int> que;
    15     while(scanf("%d",&n)!=-1)
    16     {
    17         a=0, b=0;
    18         while(!sta.empty())
    19             sta.pop();
    20         while(!que.empty())
    21             que.pop();
    22 
    23         scanf("%s%s",str1,str2);
    24         //printf("$%s$   $%s$
    ",str1,str2);
    25         while(b < n)
    26         {
    27             if(str1[a] == str2[b])
    28             {
    29                 a++; b++;
    30                 que.push(1);   //进站将“1”压进队列
    31                 que.push(0);   //出站将“0”压进队列
    32             }
    33             else if(!sta.empty() && sta.top()==str2[b])
    34             {
    35                 b++;
    36                 sta.pop();
    37                 que.push(0);
    38             }
    39             else
    40             {
    41                 sta.push(str1[a++]);
    42                 que.push(1);
    43             }
    44         }
    45         //printf("a==%d   b==%d",a,b);
    46         if(b==n && a == b)
    47         {
    48             printf("Yes.
    ");
    49             while(!que.empty())
    50             {
    51                 if(que.front()==1)
    52                     printf("in
    ");
    53                 else
    54                     printf("out
    ");
    55                 que.pop();
    56             }
    57         }
    58         else
    59             printf("No.
    ");
    60         printf("FINISH
    ");
    61 
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    jQuery(2)
    jQuery(1)
    underscore.js
    面向对象复习
    1.14函数复习
    面向对象(3)继承
    10.18
    1017
    js笔记二
    js笔记一
  • 原文地址:https://www.cnblogs.com/Dawn-bin/p/9491423.html
Copyright © 2011-2022 走看看