题面分析
次询问,
每次输入一个字符串 ,
- 如果 是
READ,将书堆上的第一本输出并移除书堆。 - 否则将 加入书堆。
核心思路
使用 stack<string> st,也就是栈,模拟书堆。
在每一次询问中:
- 如果 是
READ,输出st.top()且st.pop()。 - 否则
st.push(S)。
代码实现
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<string> st;
int q;
cin>>q;
while(q--)
{
string s;
cin>>s;
if(s=="READ")
{
cout<<st.top()<<endl;
st.pop();
}
else
st.push(s);
}
}