题解 | 验证栈序列

验证栈序列

https://www.nowcoder.com/practice/d3178fe362dd4810b577c77c9e128fc5

#include <iostream>
#include <stack>
using namespace std;
const int N = 1000010;
int a[N], b[N];

bool judeTryePopStacK(const int *a, const int *b, int length) {
    stack<int> s;
    int p = 0, q = 0;
    for (p; p < length; p++) {
        s.push(a[p]);
        while (!s.empty() && s.top() == b[q]) {
            s.pop();
            q++;
        }
    }
    return s.empty();
}

int main() {

    int n, length;
    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> length;
        for (int j = 0; j < length; j++) cin >> a[j];
        for (int j = 0; j < length; j++) cin >> b[j];

        if (judeTryePopStacK(a, b, length)) {
            cout << "Yes" << endl;
        }else {
            cout << "No" << endl;
        }
    }

    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务