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
     
    Sample Output
    1
    2
    2
    1
    1
    2
    None
    2
    3
    题意:FIFO是先进先出,FILO是先进后出。给一个n和一个命令(FIFO和FILO),下面n条语句,IN a代表a进去了,当输入OUT是你说出出来的是哪一个,如果此时没有元素的话输出None。
    用队列和栈写比较简单写。
     1 #include<iostream>
     2 #include<stack>
     3 #include<stdio.h>
     4 #include<algorithm>
     5 #include<string.h>
     6 #include<queue>
     7 #define N 100010
     8 using namespace std;
     9 #define oo 0x3f3f3f
    10 int dp[N];
    11 int a;
    12 char str[N];
    13 char s[N];
    14 int main()
    15 {
    16     int t,n;
    17     scanf("%d",&t);
    18     while(t--)
    19     {
    20         scanf("%d%s",&n,str);
    21         if(strcmp(str,"FIFO")==0)
    22         {
    23             queue<int >Q;
    24             while(n--)
    25             {
    26                 scanf("%s",s);
    27                 if(s[0]=='I')
    28                 {
    29                     scanf("%d",&a);
    30                     Q.push(a);
    31                 }
    32                 else
    33                 {
    34                     if(!Q.size()) printf("None
    ");
    35                     else
    36                     {
    37                         int k = Q.front();
    38                         Q.pop();
    39 
    40                         printf("%d
    ",k);
    41                     }
    42 
    43                 }
    44             }
    45         }
    46         else
    47         {
    48             stack<int >p;
    49             while(n--)
    50             {
    51                 scanf("%s",s);
    52                 if(s[0]=='I')
    53                 {
    54                     scanf("%d",&a);
    55                     p.push(a);
    56                 }
    57                 else
    58                 {
    59                     if(!p.size()) printf("None
    ");
    60                     else
    61                     {
    62                         int k = p.top();
    63                         p.pop();
    64                         printf("%d
    ",k);
    65                     }
    66 
    67                 }
    68             }
    69         }
    70 
    71 
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    组建小型局域网
    如何解决无法登陆微软账号
    常用网页收录入口
    Google正确搜索方法
    电脑无法识别U盘(usb类外接设备)的解决办法
    如何生成一副Poker
    Camtasia Studio8使用教程
    windows蓝屏代码大全及常见蓝屏解决方案
    [bzoj3712][PA2014]Fiolki
    [NOI2018]归程
  • 原文地址:https://www.cnblogs.com/biu-biu-biu-/p/5786732.html
Copyright © 2011-2022 走看看