20pts求助

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

Geirangerfjard @ 2022-12-27 09:51:23

原题传送门

#include <iostream>
#include <stack>
#include <cstring>

const int N = 100010;

using namespace std;

stack <int> st;
int a[N],b[N];
int T,n,len;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin >> T;
    while(T --)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        len = 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++)
        {
            st.push(a[i]);
            while(st.top()==b[len])
            {
                st.pop();
                ++len;
                if(st.empty()) break;
            } 
        }
        if(st.empty()) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

by SmileMask @ 2022-12-27 10:03:51

@Alone_Helpless

每次输入前要把栈清空

while(st.size()) st.pop();

by Geirangerfjard @ 2022-12-27 10:12:28

ok过了,感谢奆佬 @ikun_zhs


|