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

· · 题解

我们发现,这些操作和栈的操作一模一样。

于是,我们就能用栈的思想解

::::info[代码]

#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;
}

::::