跳到正文

题解

题解:P14325 [JOI2022 预选赛 R2] 图书馆 2 / Library 2

Panzx约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年10月28日
收录
2025年12月22日
更新
2026年11月14日
篇幅
约 1 分钟
题目
P14325

版本

  1. v2当前
  2. v1

题面分析

QQ 次询问,

每次输入一个字符串 SS,

  • 如果 SS 是 READ,将书堆上的第一本输出并移除书堆。
  • 否则将 SS 加入书堆。

核心思路

使用 stack<string> st,也就是栈,模拟书堆。

在每一次询问中:

  • 如果 SS 是 READ,输出st.top()且st.pop()。
  • 否则st.push(S)。

代码实现

C++
#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);
	}
}
原文