大佬们帮忙看看为什么陷入了死循环,一直没找出来

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

fufuQAQ @ 2022-03-27 21:17:39

#include<bits/stdc++.h>
#include<stack>
using namespace std;
const int N=100010; 
stack<int> x; 
int a[N],b[N],n,j;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++)
            cin>>b[i];
        j=1;
        for(int i=1;i<=n;i++)
        {
            x.push(a[i]); 
//          cout<<"a[i]="<<a[i]<<endl;
            //while((x.top()==b[j]))// && !x.empty())
            while((x.top()==b[j]) && !x.empty())
            {
//              cout<<"b["<<j<<"]="<<b[j]<<" "<<"j="<<j<<" ";
                x.pop();
//              cout<<"x.top()="<<x.top()<<endl; 
                j++;

                if(x.empty())   
                {
//                  cout<<"1"<<endl;
                    break;//栈是空的话返回1 
                }

            }
//          cout<<"i="<<i<<endl;
        } 
        if(!x.empty()) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
        while(!x.empty())
          x.pop();//清空栈 
    }   
    return 0;   
} 

by chlchl @ 2022-03-27 21:20:48

@zhejianguniversity while((x.top()==b[j]) && !x.empty())

这一句,把 !x.empty() 放到前面


by fufuQAQ @ 2022-03-28 19:22:48

@caihaolang 谢谢大佬,一直没找出来错误


|