ACboy needs your help again!(stack和queue)

原创 2018年04月16日 17:18:13


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!
InputThe 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.OutputFor 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:先进后出

思路:先进先出是队列,先进后出是栈

AC代码:


#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

int main()
{
    int T,n,m;
    char c[5],op[5];
    cin>>T;
    while(T--){
        stack<int> s;
        queue<int> q;
        cin>>n>>c;
        if(c[2]=='L'){
            for(int i=1;i<=n;++i){
                cin>>op; 
                if(op[0]=='I'){cin>>m;s.push(m);} 
                else if(op[0]=='O'&&!s.empty()){
                        cout<<s.top()<<endl;
                        s.pop();
                }
                else cout<<"None"<<endl;
            }
        }
        if(c[2]=='F'){
            for(int i=1;i<=n;++i){
                cin>>op;
                if(op[0]=='I'){cin>>m;q.push(m);}
                else if(op[0]=='O'&&!q.empty()){
                        cout<<q.front()<<endl;
                        q.pop();
                    }
                else cout<<"None"<<endl;
            }
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangxiaoduoduo/article/details/79963381

Acboy needs your help again!

ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...
  • lin14543
  • lin14543
  • 2015-07-28 18:47:49
  • 734

ACboy needs your help

Problem Description ACboy has N courses this term, and he plans to spend at most M days on study.Of...
  • xiaofang3a
  • xiaofang3a
  • 2014-05-01 20:27:58
  • 332

HDU 1072 ACboy needs your help again!(栈和队列)

ACboy needs your help again! 题目
  • Sometimes__
  • Sometimes__
  • 2014-11-10 19:45:07
  • 371

HDU 1702--ACboy needs your help again!【STL】

ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...
  • hpuhjh
  • hpuhjh
  • 2015-03-21 17:06:03
  • 217

HDU-1702-ACboy needs your help again!

HDU 1702 ACboy needs your help again! Problem Description
  • u014355480
  • u014355480
  • 2014-10-30 22:48:56
  • 737

hdu 1712 ACboy needs your help(分组背包入门题)

ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O...
  • fp_hzq
  • fp_hzq
  • 2011-10-09 16:54:46
  • 1324

ACboy needs your help 分组背包

Problem Description ACboy has N courses this term, and he plans to spend at most M days on study....
  • No_Retreats
  • No_Retreats
  • 2012-08-18 15:26:37
  • 758

hdu1702(ACboy needs your help again!) 在杭电又遇坑了

点击打开链接 结题感悟: 其实吧,这题并不是很难,就是一个栈和队列的公共题,也就是按指定的方式(栈或队列)存取数据,但是为什么我自己写的栈和队列就是不能再杭电ac,而用java包中的栈和队列就秒过了...
  • u011479875
  • u011479875
  • 2015-08-10 00:53:45
  • 1369

【杭电-oj】-1702-ACboy needs your help again!(栈和队列基础)

ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (...
  • Bear1998
  • Bear1998
  • 2016-06-30 19:03:14
  • 303

【分组背包】HDU1712ACboy needs your help

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem Description ACboy has N courses ...
  • wlxsq
  • wlxsq
  • 2016-04-09 16:22:19
  • 279
收藏助手
不良信息举报
您举报文章:ACboy needs your help again!(stack和queue)
举报原因:
原因补充:

(最多只允许输入30个字)