专栏文章

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

P14325题解参与者 3已保存评论 3

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
3 条
当前快照
1 份
快照标识符
@minh3hc1
此快照首次捕获于
2025/12/02 02:18
3 个月前
此快照最后确认于
2025/12/02 02:18
3 个月前
查看原文
我们发现,这些操作和栈的操作一模一样。
  • 书名即为将此书放在栈顶
  • READ 即为读栈顶的书,也弹出栈顶的书
于是,我们就能用栈的思想解
代码CPP
#include<iostream>
using namespace std;
struct stack{//手搓 std::stack 的部分内容
    string tmp[200005];
    int head=1,tail=0;//实际上 head 即栈底没用
    inline void push(string x){//将 x 入栈
        tmp[++tail]=x;
    }
    inline void pop(){//将栈顶弹出
        --tail;
    }
    inline string top(){//返回栈顶
        return tmp[tail];
    }
}st;
int q;
int main(){
    cin>>q;
    while(q--){
        string str;
        cin>>str;
        if(str=="READ"){//是读书
            cout<<st.top()<<'\n';
            st.pop();
        }
        else st.push(str);//放书
    }
    return 0;
}

评论

3 条评论,欢迎与作者交流。

正在加载评论...