zoukankan      html  css  js  c++  java
  • HDU--1702 ACboy needs your help again!

     ACboy was kidnapped!! 
    he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(. 
     As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy." 
    The problems of the monster is shown on the wall: 
     Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out"). 
    and the following N lines, each line is "IN M" or "OUT", (M represent a integer). 
     and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

    Input:

    The input contains multiple test cases. 
    The first line has one integer,represent the number oftest cases. 
    And the input of each subproblem are described above.

    Output:

    For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.

    Sample Input:

    4
    4 FIFO
    IN 1
    IN 2
    OUT
    OUT
    4 FILO
    IN 1
    IN 2
    OUT
    OUT
    5 FIFO
    IN 1
    IN 2
    OUT
    OUT
    OUT
    5 FILO
    IN 1
    IN 2
    OUT
    IN 3
    OUT
    Output:
    1
    2
    2
    1
    1
    2
    None
    2
    3
    AC代码:
     1 #include<stdio.h>
     2 #include<stack>
     3 #include<queue>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,m;
     8     char c[5];
     9     scanf("%d",&n);
    10     while(n--)
    11     {scanf("%d %s",&m,c);
    12      queue<int>q;
    13      stack<int>s;
    14      while(m--)
    15        {  if(c[2]=='F')//队列 
    16           {
    17            char d[4];
    18            int j;
    19            scanf("%s",d);
    20            if(d[0]=='I')
    21             {scanf("%d",&j);
    22              q.push(j);
    23                
    24             }
    25           else
    26             {if(q.empty()) printf("None
    ");
    27              else 
    28               {int x;
    29                x=q.front();
    30                printf("%d
    ",x);
    31                q.pop();
    32                   
    33               }
    34                
    35             }  
    36             
    37           }
    38           else        //
    39            { 
    40              char e[4];
    41              int k;
    42              scanf("%s",e);
    43              if(e[0]=='I')
    44                {scanf("%d",&k);
    45                 s.push(k);
    46                
    47                }
    48              else
    49               {if(s.empty()) printf("None
    ");
    50                else 
    51                 {int y;
    52                  y=s.top();
    53                  printf("%d
    ",y);
    54                  s.pop();
    55                   
    56                 }
    57                
    58               }  
    59             
    60             
    61            }        
    62            
    63        }
    64         
    65     }
    66     return 0;
    67 }
    
    
    
    
  • 相关阅读:
    ZOJ 3631 Watashi's BG(dp+dfs)
    hdu 1506 Largest Rectangle in a Histogram(单调栈)
    csu 1392 Number Trick (数论)
    ACM 奋斗的小蜗牛
    ACM 16进制的简单运算
    ACM 交换输出
    ACM Longest Repeated Sequence
    ACM Arithmetic Expression
    ACM 素数
    ACM 无线网络覆盖
  • 原文地址:https://www.cnblogs.com/hss-521/p/7235810.html
Copyright © 2011-2022 走看看