• 02-线性结构4 Pop Sequence


    02-线性结构4 Pop Sequence   (25分)

    • 时间限制:400ms
    • 内存限制:64MB
    • 代码长度限制:16kB
    • 判题程序:系统默认
    • 作者:陈越
    • 单位:浙江大学

    https://pta.patest.cn/pta/test/3512/exam/4/question/62615

    Given a stack which can keep MMM numbers at most. Push NNN numbers in the order of 1, 2, 3, ..., NNN and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if MMM is 5 and NNN is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): MMM (the maximum capacity of the stack), NNN (the length of push sequence), and KKK (the number of pop sequences to be checked). Then KKK lines follow, each contains a pop sequence of NNN numbers. All the numbers in a line are separated by a space.

    Output Specification:

    For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

    Sample Input:

    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
    

    Sample Output:

    YES
    NO
    NO
    YES
    NO

    用回溯法(dfs)判断能否构造出目标序列
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int gode[1000];
     5 int M,N,K,num;
     6 stack<int> Sta;
     7 
     8 bool dfs(int ipush,int ipop)//ipush,ipop为已完成的操作(push,pop)数
     9 {
    10     if(ipop==N)//成功构造gode
    11         return true;
    12     if(ipush<N&&Sta.size()<M)
    13     {
    14         Sta.push(num++);
    15         if(dfs(ipush+1,ipop)) return true;
    16         num--;
    17         Sta.pop();
    18     }
    19     if(ipop<ipush&&!Sta.empty()&&Sta.top()==gode[ipop])//栈顶元素即为gode的下一个数时才执行pop
    20     {
    21         int t=Sta.top();
    22         Sta.pop();
    23         if(dfs(ipush,ipop+1)) return true;
    24         Sta.push(t);
    25     }
    26     return false;
    27 }
    28 int main()
    29 {
    30     cin>>M>>N>>K;
    31     for(int i=0;i<K;i++)
    32     {
    33         num=1;
    34         for(int j=0;j<N;j++)
    35             cin>>gode[j];
    36         puts(dfs(0,0)?"YES":"NO");
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    bootstrap table load属性
    Jquery中hide()、show()、remove()特性
    Jquery精准计算
    .nojekyll 文件是什么
    快来用 Nuxt 开发静态网站
    CI:持续集成
    把组件库发布到 npm
    JS的各种模块化规范
    打包发布:让组件库支持按需加载
    docz: 组件库文档 so easy!
  • 原文地址:https://www.cnblogs.com/Fresh--air/p/6725343.html
Copyright © 2020-2023  润新知