样例能过,但是全WA

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

yushui666 @ 2024-12-06 14:41:51


#include <iostream>
#include <iomanip>
#include <queue>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;

int note[100002];

int main()
{
    int n, m, w, p;

    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        stack<int> q;
        queue<int> x;
        int point = 0;
        cin >> m;

        for (int j = 0; j < m; j++)
        {
            cin >> w;
            q.push(w);
        }
        for (int k = 0; k < m; k++)
        {
            cin >> p;
            x.push(p);
        }

        while (!q.empty() && !x.empty())
        {
            int xxx = q.top();
            q.pop();
            int yyy = x.front();
            x.pop();
            if (xxx != yyy)
            {
                point = 1;
                break;
            }
        }

        if (point == 1)
        {
            cout << "No" << endl;
        }
        else
        {
            cout << "Yes" << endl;
        }
    }

    return 0;
}

by Diary_Of_Young @ 2024-12-06 14:45:20

建议手写栈

#include<bits/stdc++.h>

using namespace std;

const int N=1e6+10;
int stk[N];

int pushed[N],poped[N];

int endm=0;

void out()
{
    endm--;
}
void in(int x)
{
    stk[++endm]=x;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    while(n--)
{
    int x;
    cin>>x;
    for(int i=1;i<=x;i++)
    {
        cin>>pushed[i];
    }

    for(int i=1;i<=x;i++)
    {
        cin>>poped[i];
    }
    endm=0;
    int y=1;
    for(int i=1;i<=x;i++)
    {       
        in(pushed[i]);
        for(int j=endm;endm!=0;j--)
        {   
            if(stk[j]==poped[y])
            {
                y++;
                out();
            }
            else
            {
                break;
            }
        }
    }

    if(endm==0)
    {
        cout<<"Yes"<<endl;
    }
    else
    {
        cout<<"No"<<endl;
    }
}
    return 0;
} 

by yushui666 @ 2024-12-06 14:59:35

理解错了题目,此帖结


|