题解:UVA13055 Inception

· · 题解

思路

栈的模板题。

题目中给出三种操作。

按照思路模拟即可。

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    stack <string> s;
    string a;
    while(n--)
    {
        cin>>a;
        if(a=="Sleep")
        {
            cin>>a;
            s.push(a);
        }
        else if(a=="Kick")
        {
            if(!s.empty()) s.pop();
        }
        else if(a=="Test")
        {
            if(!s.empty()) cout<<s.top<<endl;
            else cout<<"Not in a dream\n";
        }
    }
    return 0;
}