zoukankan      html  css  js  c++  java
  • 出栈序列的合法性 【模拟】

    7-15 出栈序列的合法性(25 分)

    给定一个最大容量为 M 的堆栈,将 N 个数字按 1, 2, 3, …, N 的顺序入栈,允许按任何顺序出栈,则哪些数字序列是不可能得到的?例如给定 M=5、N=7,则我们有可能得到{ 1, 2, 3, 4, 5, 6, 7 },但不可能得到{ 3, 2, 1, 7, 5, 6, 4 }。
    输入格式:

    输入第一行给出 3 个不超过 1000 的正整数:M(堆栈最大容量)、N(入栈元素个数)、K(待检查的出栈序列个数)。最后 K 行,每行给出 N 个数字的出栈序列。所有同行数字以空格间隔。
    输出格式:

    对每一行出栈序列,如果其的确是有可能得到的合法序列,就在一行中输出YES,否则输出NO。
    输入样例:

    5 7 5
    1 2 3 4 5 6 7
    3 2 1 7 5 6 4
    7 6 5 4 3 2 1
    5 6 4 3 7 2 1
    1 7 6 5 4 3 2

    输出样例:

    YES
    NO
    NO
    YES
    NO

    思路
    用一个队列保存 读入的数据
    然后 用一个堆栈 来模拟
    按 1.2.3…n 的顺序 入栈
    当栈顶元素与队首元素相同时 就分别出栈 和出队
    当 入栈元素超过 M 时 就跳出
    最后 判断 是不是 入了 N 个元素 并且 栈空 就是 YES

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-6;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e3 + 5;
    const int MOD = 1e9 + 7;
    
    int main()
    {
        int m, n, k;
        queue <int> q;
        stack <int> s;
        scanf("%d%d%d", &m, &n, &k);
        for (int i = 0; i < k; i++)
        {
            while (!q.empty())
                q.pop();
            while (!s.empty())
                s.pop();
            int num;
            for (int j = 0; j < n; j++)
            {
                scanf("%d", &num);
                q.push(num);
            }
            int vis = 1;
            while (s.size() < m && !q.empty())
            {
                s.push(vis);
                vis++;
                while (!s.empty() && !q.empty() && s.top() == q.front())
                {
                    s.pop();
                    q.pop();
                }
                if (vis == n + 1)
                    break;
            }
            if (vis == n + 1 && s.empty())
                printf("YES
    ");
            else
                printf("NO
    ");
        }
    }
  • 相关阅读:
    入门(一)---Java的发展史
    移除元素
    TCP的 “三次握手” 和“四次挥手”,到底是什么鬼?
    功能测试框架
    python学习笔记之--__new__方法和__init__方法
    HTTP协议状态码详解
    python学习笔记之--hasattr函数
    一文总结软件测试工程师面试前必背的面试题(持续更新中)
    MYSQL安装file /usr/share/mysql/charsets/README from install of MySQL-server-5.6.35-1.el6.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64报错
    centos7 安装salt起不来处理
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433213.html
Copyright © 2011-2022 走看看