美文网首页
2020-07-02 【深基15.习9】验证栈序列

2020-07-02 【深基15.习9】验证栈序列

作者: JalorOo | 来源:发表于2020-07-02 22:57 被阅读0次

    题目:https://www.luogu.com.cn/problem/P4387

    #include<iostream>
    #include<cstdio>
    #include<stack>
    #define MAXN 100005
    using namespace std;
    
    int stkA[MAXN];
    int stkB[MAXN];
    
    int read(){
        int x = 0,f = 1;
        char c = getchar();
        while (c<'0'||c>'9') {
            if (c=='-') {
                f = -1;
            }
            c = getchar();
        }
        while (c>='0'&&c<='9') {
            x = x*10+c-'0';
            c = getchar();
        }
        return x*f;
    }
    
    int main(){
        int n = read();
        while(n--){
            int num = read();
            stack<int> s;
            int A = 1,B = 1;
            for (int i = 1; i<=num; i++) {
                stkA[i] = read();
            }
            for (int i = 1; i<=num; i++) {
                stkB[i] = read();
            }
            while(A<=num){
                s.push(stkA[A]);
                while(stkB[B]==s.top()) {
                    s.pop();
                    B++;
                    if (s.empty()) {
                        break;
                    }
                }
                A++;
            }
            printf("%s\n",s.empty()?"Yes":"No");
        }
        return 0;
    }
    /*
    1
    5
    1 5 2 3 4
    2 4 3 5 1
    */
    
    

    相关文章

      网友评论

          本文标题:2020-07-02 【深基15.习9】验证栈序列

          本文链接:https://www.haomeiwen.com/subject/wrndqktx.html