HDU 1702 ACboy needs your help again! (栈和队列的模拟)

ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3164    Accepted Submission(s): 1655

Problem Description

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

Source

2007省赛集训队练习赛(1)

Recommend

lcy

就是对栈和队列的模拟,栈是FILO队列是FIFO

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<stack>
 5 #include<string>
 6 using namespace std;
 7 int main()
 8 {
 9     int kase,n,num;
10     string s,str;
11     cin>>kase;
12     while(kase--)
13     {
14         cin>>n>>s;
15         if(s=="FIFO")
16         {
17             queue<int> Q;
18             while(n--)
19             {
20                 cin>>str;
21                 if(str=="IN")
22                 {
23                     cin>>num;
24                     Q.push(num);
25                 }
26                 if(str=="OUT")
27                 {
28                     if(Q.empty())
29                         printf("None\n");
30                     else
31                     {
32                         printf("%d\n",Q.front());
33                         Q.pop();
34                     }
35                 }
36             }
37         }
38         if(s=="FILO")
39         {
40             stack<int> S;
41             while(n--)
42             {
43                 cin>>str;
44                 if(str=="IN")
45                 {
46                     cin>>num;
47                     S.push(num);
48                 }
49                 if(str=="OUT")
50                 {
51                     if(S.empty())
52                         printf("None\n");
53                     else
54                     {
55                         printf("%d\n",S.top());
56                         S.pop();
57                     }
58                 }
59             }
60         }
61     }
62     return 0;
63 }

HDU 1702 ACboy needs your help again! (栈和队列的模拟),布布扣,bubuko.com

时间: 2024-10-06 01:17:49

HDU 1702 ACboy needs your help again! (栈和队列的模拟)的相关文章

HDU 1702 ACboy needs your help again! (栈与队列)

ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3701    Accepted Submission(s): 1885 Problem Description ACboy was kidnapped!! he miss his mother very much and is v

hdu 1702 ACboy needs your help again!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1702 题目大意:按照所给要求,输出相应的数.“FIFO”指代先进先出,即队列的概念,“FILO”指代先进后出,即栈的表现形式~这里定义两个函数即可,一个队列,一个栈的调用! 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <stack> 5 #include <

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

Description 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 g

HDU 1702 ACboy needs your help again!(栈 队列 基础)

#include<cstdio> #include<cmath> #include<queue> #include<stack> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int t,n; int main() { int i,j,k; cin>>t; char op[10],io[10]; whi

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 ma

java 栈和队列的模拟--java

栈的定义:栈是一种特殊的表这种表只在表头进行插入和删除操作.因此,表头对于栈来说具有特殊的意义,称为栈顶.相应地,表尾称为栈底.不含任何元素的栈称为空栈. 栈的逻辑结构:假设一个栈S中的元素为an,an-1,..,a1,则称a1为栈底元素,an为栈顶元 素.栈中的元素按a1 ,a2,..,an-1,an的次序进栈.在任何时候,出栈的元素都是栈顶元素.换句话说,栈的修改是按后进先出的原则进行的.因此,栈又称为后进先出(Last In First Out)表,简称为LIFO表.所以,只要问题满足LI

(hdu step 8.1.1)ACboy needs your help again!(STL中栈和队列的基本使用)

题目: ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 73 Accepted Submission(s): 57   Problem Description ACboy was kidnapped!! he miss his mother very much and is very

自己设置的纸牌游戏(简单数组栈和队列的设计)

星期天小白与小婷约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏--"小猫钓鱼".游戏的规则是这样的:将一副扑克牌平均分成俩份,每人拿一份.小白先拿出手中的第一张扑克牌放在桌上,然后小婷也拿出手中的第一张扑克牌,并放在小白刚打出的扑克牌上面,就像这样俩人交替出牌.出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将俩张相同的牌以及中间所夹着的牌全部取走,并依次放到自己手中牌的末尾.当任意一个人的牌全部出完时,游戏结束,对手获胜! 现在,我为了方便测试只涉及一组数据,因为数据如果不对的

HDOJ 1702 ACboy needs your help again!

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1702 ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3512    Accepted Submission(s): 1797 Problem Description ACboy w