80分求助

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

Willan_Lian @ 2023-07-21 15:06:14

代码如下:


#include<bits/stdc++.h>
using namespace std;
stack <int > st;
int q,n;
int a[100010],b[100010];
int main(){
    scanf("%d",&q);
    while(q--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);

        int j=1;
        for(int i=1;i<=n;i++){
            st.push(a[i]);
            while (!st.empty() && st.top()==b[j])
                st.pop(),j++;
        }
        if(j==n+1) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

by zhangyuanxiao @ 2023-07-21 15:09:00

@Willan_Lian 有没有可能是你栈没清空


by zhangyuanxiao @ 2023-07-21 15:10:08

加个

while(!st.empty()) st.pop();

by Willan_Lian @ 2023-07-21 15:15:17

谢谢!orz


|