ACboy needs your help again!--hdu1702

ACboy needs your help again!

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

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

3

Sample Output

1

2

2

1

1

2

None

2

3

这个题大意就是:FIFO代表是先输入的数先输出,FILO代表的是先输入的数后输出。没有数据就输出None!

很容易想到用栈和队列来解决问题。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stack> 4 #include<queue> 5 using namespace std;
 6 char s1[10],s2[10];
 7 int main()
 8 {
 9     int n,a,b;
10
11     scanf("%d",&n);
12     while(n--)
13     {
14         queue<int>s;//定义一个队列
15         while(!s.empty())
16         s.pop();
17         stack<int>r;//定义一个栈
18         while(!r.empty())19         r.pop();//一定要每次都要清空队列,要不然各种WA我也是醉了!
20         scanf("%d %s",&a,s1);
21         getchar();//注意吸收回车
22         if(strcmp(s1,"FIFO")==0)
23         {
24             while(a--)
25             {
26                 scanf("%s",s2);
27                 if(strcmp(s2,"IN")==0)
28                 {
29                     scanf("%d",&b);
30                     getchar();
31                     s.push(b);
32                  }
33                  else if(strcmp(s2,"OUT")==0)
34                  {
35                      if(s.empty())
36                      printf("None\n");
37                      else
38                      {
39                          printf("%d\n",s.front());//输出队首元素
40                          s.pop();
41                      }
42
43                  }
44             }
45         }
46         else if(strcmp(s1,"FILO")==0)
47         {
48             while(a--)
49             {
50                 scanf("%s",s2);
51                 if(strcmp(s2,"IN")==0)
52                 {
53                     scanf("%d",&b);
54                     getchar();
55                     r.push(b);
56                  }
57                  else if(strcmp(s2,"OUT")==0)
58                  {
59                      if(r.empty())
60                      printf("None\n");
61                      else
62                      {
63                          printf("%d\n",r.top());//输出栈顶元素
64                          r.pop();
65                      }
66
67                  }
68             }
69         }
70     }
71     return 0;
72 }
时间: 2024-07-30 08:22:13

ACboy needs your help again!--hdu1702的相关文章

hdu1702 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): 11427    Accepted Submission(s): 5750 Problem Description ACboy was kidnapped!! he miss his mother very much and is v

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

点击打开链接 结题感悟: 其实吧,这题并不是很难,就是一个栈和队列的公共题,也就是按指定的方式(栈或队列)存取数据,但是为什么我自己写的栈和队列就是不能再杭电ac(一直wa啊),而用java包中的栈和队列就秒过了,问题尚未找出原因,值得思考啊.不过可以趁此学学这两个类(尽量还是自己动手写的好啊) 栈:java.util 类 Stack<E> Stack 类表示后进先出(LIFO)的对象堆栈.它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈.它提供了通常的push 和 pop

HDUOJ P1702 ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8599    Accepted Submission(s): 4306 Problem Description ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image

(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

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

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 <

hdu1561--H - ACboy needs your help(树形dp)

H - ACboy needs your help Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from diffe

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

HDU1712:ACboy needs your help(分组背包模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem Description ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrang