help!!!

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

chenjieyang @ 2022-11-14 21:18:45

//P1160 队列安排
#include<iostream>
#include<algorithm>
#include<stack>
#define ll long long 
using namespace std;
ll q,n,a[100010],b[100010];
int main(){
    cin>>q;
    while(q--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        for(int i=1;i<=n;i++){
            cin>>b[i];
        }
        bool flag=1;
        stack<ll>s;
        ll cnt=1;
        for(int i=1;i<=n;i++){
            s.push(a[i]);
            while(s.top()==b[cnt]){
                cnt++;
                s.pop();
            }
        }
        if(s.empty()){
            cout<<"Yes"<<endl;
        }else{
            cout<<"No"<<endl;
        }
    }
    return 0;
}

by JoeZ009 @ 2022-12-04 15:49:48

第23行 while(s.top()==b[cnt])

改为 while(s.size()&&s.top()==b[cnt])试试


|