全RE球助

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

lucy2012 @ 2024-04-27 20:48:20

QwQ

#include<bits/stdc++.h>
using namespace std;
int t,n,b1[1000010],b2[1000010];
stack<int> a;
int main(){
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>n;
        for(int j=1;j<=n;j++)
            cin>>b1[j];
        for(int j=1;j<=n;j++)
            cin>>b2[j];
        int cnt=1;
        a.push(b1[cnt++]);
        for(int j=1;j<=n;j++){
            while(!a.empty()&&a.top()!=b2[j]&&cnt<=n){
                a.push(b1[cnt]);
                cnt++;
            }
            if(a.top()==b2[j]) a.pop();
        }
        if(a.empty())
            cout<<"Yes"<<endl;
        else 
            cout<<"No"<<endl;
        while(!a.empty()){
            a.pop();
        }
    }
    return 0;
}

by I_Love_DS @ 2024-04-27 22:19:20

#include <bits/stdc++.h>
using namespace std;
int q,n;
int push[100050],pop[100050],sum;
stack <int> s;
int main(){
    scanf("%d",&q);
    for (int i = 1; i <= q; i++) {
        scanf("%d",&n);
        memset(push,0,sizeof(push));
        memset(pop,0,sizeof(pop));
        sum = 1;
        for (int i = 1; i <= n; i++) scanf("%d",&push[i]);
        for (int i = 1; i <= n; i++) scanf("%d",&pop[i]);
        for (int i = 1; i <= n; i++) {
            s.push(push[i]);
            while (pop[sum] == (s.top())) {
                s.pop();
                sum++;
                if (s.empty()) break;
            }
        }
        if (s.empty()) printf("Yes\n");
        else printf("No\n");
        while (!s.empty()) s.pop();
    }
    return 0;
}

by I_Love_DS @ 2024-04-27 22:21:23

报错翻译:

运行时错误。 接收信号11:内存引用无效的分段故障。


by I_Love_DS @ 2024-04-27 22:22:29

有没有可能你空栈时使用了 top() 语句


by I_Love_DS @ 2024-04-27 22:23:37

if(a.top()==b2[j]) a.pop();

这句可能栈为空访问top,直接RE

@lucy2012


by lucy2012 @ 2024-04-27 22:25:17

@liuruiqing ok,谢谢啦!


|