求助

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

CZKWEN543 @ 2022-10-15 08:38:01

#include<bits/stdc++.h>
using namespace std;

stack<int>q;
int p, n;
int a[100005], b[100005], sum=1;

int main(){
    cin>>p;
    while(p--){
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=n;i++) cin>>b[i];
        for(int i=1;i<=n;i++){
            q.push(a[i]);
            while(q.top()==b[sum]){
                q.pop();
                sum++;
                if(q.empty()) break;
            }
        }
        if(q.empty()) cout<<"Yes\n";
        else cout<<"No\n";
        while(!q.empty()) q.pop();
    }
    return 0;
}

by Kevin_Mamba @ 2022-10-15 08:52:30

有多组数据 。


by Kevin_Mamba @ 2022-10-15 09:05:19

@CZKWEN543


by CZKWEN543 @ 2022-10-15 09:34:21

灰常感谢qp


by CZKWEN543 @ 2022-10-15 09:34:37

此贴完结


|