题解:P14325 [JOI2022 预选赛 R2] 图书馆 2 / Library 2
Panzx
·
·
题解
题面分析
每次输入一个字符串 $S$,
- 如果 $S$ 是 `READ`,将书堆上的第一本输出并移除书堆。
- 否则将 $S$ 加入书堆。
## 核心思路
使用 `stack<string> st`,也就是**栈**,模拟书堆。
在每一次询问中:
- 如果 $S$ 是 `READ`,输出`st.top()`且`st.pop()`。
- 否则`st.push(S)`。
## 代码实现
```cpp
#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);
}
}
```