题目描述:
ACboy被绑架了!!
他非常想念他的母亲,现在非常害怕。你无法想象他被安置在多么黑暗的房间里,好可怜:
作为一个聪明的 ACMer,你想把 ACboy 从怪物的迷宫中救出来。但是当你到了迷宫门口,怪物说:“我听说你很聪明,但如果不能解决我的问题,你会和 ACboy 一起死。”
怪物的问题显示在墙上:
每个问题的第一行是一个整数N(命令的数量),和一个单词“FIFO”或“FILO”。(你很高兴,因为你知道“FIFO”代表“先进先出”,“FILO”表示“First In Last Out").
和后面的 N 行,每行是 "IN" 或 "OUT",
而问题的答案是一道门的密码,所以如果你想解救ACboy,请认真回答问题!
输入
输入包含多个测试用例。
第一行有一个整数,代表测试用例的个数。
上面描述了每个子问题的输入。
输出
对于每个命令“OUT”,你应该输出一个整数,取决于单词是“FIFO”还是“FILO”,或者如果你没有任何整数,则输出一个单词“None”。
样式输入
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
样式输出
1
2
2
1
1
2
None
2
3
思路:
“FIFO”(First In First Out)就是队列,“FILO”(First In Last Out) 就是栈。使用STL的stack和queue直接进行模拟即可。
#include <iostream>
using namespace std;
#include <stack>
#include <queue>
int main()
{
int t,n,temp;
cin>>t;
while(t--){
string str,str1;
queue<int> q;
stack<int> s;
cin>>n>>str;
for(int i=0;i<n;i++)
{
if(str=="FIFO"){
cin>>str1;
if(str1=="IN"){
cin>>temp;
q.push(temp);
}
if(str1=="OUT"){
if(q.empty()){
cout<<"None"<<endl;
}
else{
cout<<q.front()<<endl;
q.pop();
}
}
}
if(str=="FILO"){
cin>>str1;
if(str1=="IN"){
cin>>temp;
s.push(temp);
}
if(str1=="OUT"){
if(s.empty()){
cout<<"None"<<endl;
}
else{
cout<<s.top()<<endl;
s.pop();
}
}
}
}
}
return 0;
}