洛谷B3614 【模板】栈 题解

题目链接:B3614 【模板】栈

题目描述:

请你实现一个栈(stack),支持如下操作: - push(x):向栈中加入一个数 \(x\)。 - pop():将栈顶弹出。如果此时栈为空则不进行弹出操作,输出 Empty。 - query():输出栈顶元素,如果此时栈为空则输出 Anguei!。 - size():输出此时栈内元素个数。

输入格式:

本题单测试点内有多组数据。

输入第一行是一个整数 \(T\),表示数据组数。对于每组数据,格式如下:

每组数据第一行是一个整数,表示操作的次数 \(n\)。

接下来 \(n\) 行,每行首先由一个字符串,为 push,pop,query 和 size 之一。若为 push,则其后有一个整数 \(x\),表示要被加入的数,\(x\) 和字符串之间用空格隔开;若不是 push,则本行没有其它内容。

输出格式:

对于每组数据,按照「题目描述」中的要求依次输出。每次输出占一行。

数据范围:

保证 \(1 \leq T, n\leq 10^6\),且单个测试点内的 \(n\) 之和不超过 \(10^6\),即 \(\sum n \leq 10^6\)。保证 \(0 \leq x \lt 2^{64}\)。

本题唯一坑点:\(0 \leq x < 2^{64}\) ,需要使用 unsigned long long ,就很……你懂吧。

本文给出三种实现方法,分别是 stack、vector 和 数组实现,在本题里常数是差不多的。

时间复杂度 \(O(\sum q)\) ,本人其实推荐用 vector,stack 很少用得到而且清空很慢。

STL stack 代码:

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
template<typename T> void up(T &x, T y) { x < y ? x = y : 0; }
template<typename T> void down(T &x, T y) { x > y ? x = y : 0; }
#define rep(i, a, b) for(int i = (a), i##END = (b); i <= i##END; i++)
#define Rep(i, a, b) for(int i = (a), i##END = (b); i >= i##END; i--)
#define N ((int)())

stack<ull> stk;
void solve()
{
    while(not stk.empty()) stk.pop();
    int q; cin >> q;
    while(q--)
    {
        static string str; cin >> str;
        if(str == "push")
        {
            ull x; cin >> x; stk.push(x);
        }else if(str == "query")
        {
            if(stk.empty()) { cout << "Anguei!\n"; }
            else { cout << stk.top() << '\n'; }
        }else if(str == "pop")
        {
            if(stk.empty()) { cout << "Empty\n"; }
            else { stk.pop(); }
        }else
        {
            assert(str == "size");
            cout << stk.size() << '\n';
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    int qwq; cin >> qwq; while(qwq--) solve();
    return 0;
}

STL vector 代码:

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
template<typename T> void up(T &x, T y) { x < y ? x = y : 0; }
template<typename T> void down(T &x, T y) { x > y ? x = y : 0; }
#define rep(i, a, b) for(int i = (a), i##END = (b); i <= i##END; i++)
#define Rep(i, a, b) for(int i = (a), i##END = (b); i >= i##END; i--)
#define N ((int)())

vector<ull> vec;
void solve()
{
    vec.clear(); int q; cin >> q;
    while(q--)
    {
        static string str; cin >> str;
        if(str == "push")
        {
            ull x; cin >> x; vec.push_back(x);
        }else if(str == "query")
        {
            if(vec.empty()) { cout << "Anguei!\n"; }
            else { cout << *vec.rbegin() << '\n'; }
        }else if(str == "pop")
        {
            if(vec.empty()) { cout << "Empty\n"; }
            else { vec.pop_back(); }
        }else
        {
            assert(str == "size");
            cout << vec.size() << '\n';
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    int qwq; cin >> qwq; while(qwq--) solve();
    return 0;
}

数组实现代码:

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
template<typename T> void up(T &x, T y) { x < y ? x = y : 0; }
template<typename T> void down(T &x, T y) { x > y ? x = y : 0; }
#define rep(i, a, b) for(int i = (a), i##END = (b); i <= i##END; i++)
#define Rep(i, a, b) for(int i = (a), i##END = (b); i >= i##END; i--)
#define N ((int)(1e6 + 15))

ull stk[N];
void solve()
{
    int top = 0; int q; cin >> q;
    while(q--)
    {
        static string str; cin >> str;
        if(str == "push")
        {
            ull x; cin >> x; stk[++top] = x;
        }else if(str == "query")
        {
            if(top == 0) { cout << "Anguei!\n"; }
            else { cout << stk[top] << '\n'; }
        }else if(str == "pop")
        {
            if(top == 0) { cout << "Empty\n"; }
            else { --top; }
        }else
        {
            assert(str == "size");
            cout << top << '\n';
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    int qwq; cin >> qwq; while(qwq--) solve();
    return 0;
}