A - I
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
- 题意:栈与队列的基本运用。
- 思路:用栈和队列解决。
- 失误:没有清空栈和队列。
- 代码如下:
#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<cstring>
#include<functional>
using namespace std;
int main()
{
int t,i,n,elem;
char str[39],str1[89],str2[99];
queue<int> qu;
stack<int> sta;
cin>>t;
while(t--)
{
cin>>n>>str;//输入字符串时越过空格读空格后的字符直到读到另一空格 然后在str后置'\0'.
//cin.getline可以读取空格,和gets的用法相似无限读取
for(i=1;i<=n;++i)
{
if(strcmp(str,"FIFO")==0)
{
cin>>str1;
if(strcmp(str1,"IN")==0)
{
cin>>elem;
qu.push(elem);
}
else
{
if(qu.empty())
cout<<"None"<<endl;
else
{
cout<<qu.front()<<endl;
qu.pop();
}
}
}
else
{
cin>>str2;
if(strcmp(str2,"IN")==0)
{
cin>>elem;
sta.push(elem);
}
else
{
if(sta.empty())
cout<<"None"<<endl;
else
{
cout<<sta.top()<<endl;
sta.pop();
}
}
}
}
while(!qu.empty()) //清空栈和队列
qu.pop();
while(!sta.empty())
sta.pop();
}
return 0;
}
本文介绍了一个ACM竞赛题目,该题目要求利用栈和队列解决迷宫问题,通过处理不同的命令来输出相应的数值或状态。文章提供了完整的代码实现,并指出了在实现过程中容易犯的错误。

被折叠的 条评论
为什么被折叠?



