求助大佬!

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

ybc2026_sutongyan @ 2023-11-21 20:50:45

自己试了几个也没问题,为啥全错了!

#include<bits/stdc++.h>
using namespace std;
int a[100001];
int s[100001];
int main() {
    int n;
    int kkk;
    cin>>kkk;
    while(kkk--) {
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>s[i];
        }
        for(int i=0;i<n;i++){
            cin>>a[i];
        }

        stack <int> t;
        int i=0,xi=0;
        while(1) {
            if(i<n)t.push(s[i]);
            if(t.top()!=a[xi]) {
                if(i>=n) {
                    cout<<"NO"<<endl;
                    break;
                }
                i++;
            } else {
                t.pop();
                xi++;
                i++;
            }
            if(xi==n) {
                cout<<"YES"<<endl;
                break;
            }
        }
    }
    return 0;
}

by DKsniper @ 2023-11-28 20:12:52

@fish2012

你可以康康我的代码

#include<bits/stdc++.h>
using namespace std;
int q,n,a[100010],b[100010];
stack<int> s;
int main()
{
    cin>>q;
    while(q--)
    {
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        int t=1;
        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++)
        {
            s.push(a[i]);
            while(s.top()==b[t])
            {
                s.pop();
                t++;
                if(s.empty()) break;
            }
        }
        if(!s.empty()) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
        while(!s.empty()) s.pop();
    }
    return 0;
}

by derekxue @ 2023-11-30 11:46:36

不是YES,NO 而是Yes,No


by derekxue @ 2023-11-30 12:29:02

6
3
1 2 3
1 2 3
3
1 2 3
1 3 2
3
1 2 3
3 2 1
3
1 2 3
2 1 3
3
1 2 3
2 3 1
3
1 2 3
3 1 2


by derekxue @ 2023-11-30 12:31:04

用这个用例,你的输出是
Yes
Yes
Yes
No
Yes
No
应该是
Yes
Yes
Yes
Yes
Yes
No


|