P4387 P4387 【深基15.习9】验证栈序列

题目传送门

C++代码

#include<bits/stdc++.h>

using namespace std;
const int N = 100010;
int a[N], b[N];
int stk[N], tt;
int n, q;

//纯模拟算法
int main() {
    cin >> q;
    while (q--) {
        //清空栈
        tt = 0;

        //输入
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int i = 1; i <= n; i++) cin >> b[i];

        int head = 1;
        for (int i = 1; i <= n; i++) {
            stk[++tt] = a[i];
            while (stk[tt] == b[head]) {
                tt--;
                head++;
                if (tt == 0) break;
            }
        }
        if (tt == 0) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}
原文地址:https://www.cnblogs.com/littlehb/p/15075670.html