栈模拟 洛谷 P4387验证栈序列

洛谷 P4387

验证栈序列

AC代码

#include<bits/stdc++.h>
using namespace std;
int main() {
    int q; cin >> q;
    for (int i = 0; i < q; i++) {
        int n, x; cin >> n;
        stack<int> pushed;
        vector<int> a, b;
        for (int j = 0; j < n; j++) {
            cin >> x;
            a.push_back(x);
        }
        for (int j = 0; j < n; j++) {
            cin >> x;
            b.push_back(x);
        }
        int count = 0;
        for (int j = 0; j < n; j++) {
            pushed.push(a[j]);
            while (pushed.top() == b[count]) {
                pushed.pop(); count++;
                if (pushed.empty()) break;
            }
        }
        if (pushed.empty()) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}

posted @ 2024-05-22 16:45  若把你比作歌  阅读(18)  评论(0)    收藏  举报