跳到正文

个人记录

wangjuncheng

JiaXiaYuanJun约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月29日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;
bool isstack(int pushed[], int popped[], int n)
{
    stack<int> s;
    int index = 0;
    for (int i = 0; i < n; ++i)
	{
        s.push(pushed[i]);
        while (!s.empty() && s.top() == popped[index]) {
            s.pop();
            ++index;
        }
    }
    return s.empty();
}
int main()
{
    int T;
    cin >> T;
    while (T--)
	{
        int n;
        cin >> n;
        int pushed[n], popped[n];
        for (int i = 0; i < n; ++i) cin >> pushed[i];
        for (int i = 0; i < n; ++i) cin >> popped[i];
        if (isstack(pushed, popped, n)) cout<<"Yes"<<endl;
        }
		else cout<<"No"<<endl;
    }
    return 0;
}
原文