1051. Pop Sequence (25)
Given a stack which can keep M numbers at the most. Push n numbers in the order of 1, 2, 3, ..., N and pop randomly. You were supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N are 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 the one test case. For each case, the first line contains 3 numbers (all no more than): M (the maximum capacity of the stack), N (The Le Ngth of Push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line is separated by a space.
Output Specification:
For each pop sequence, print on one line "YES" if it is indeed a possible pops sequence of the stack, or "NO" if not.
Sample Input:
5 7 51 2 3 4 5 6 73 2 1 7 5 6 47 6 5 4 3 2 15 6 4 3 7 2 11 7 6 5 4 3 2
Sample Output:
Yesnonoyesno
1#include <iostream>2#include <stack>3 4 using namespacestd;5 6 intsequence[1001];7 8 intMain ()9 {Ten intmaxcapacity, Seqlength, Querynum; OneCIN >> maxcapacity >> seqlength >>Querynum; A - for(inti =0; i < Querynum; i++) - { thestack<int>Stack; - for(intj =0; J < Seqlength; J + +) -CIN >>Sequence[j]; - intStart =1; + intj =0; - while(J <seqlength) + { A while(Start <=Sequence[j]) at { - Stack.push (start); -start++; - if(stack.size () = =maxcapacity) - Break; - } in if(Stack.top ()! =Sequence[j]) - Break; to Stack.pop (); +J + +; - } the if(J = =seqlength) *cout <<"YES"<<Endl; $ ElsePanax Notoginsengcout <<"NO"<<Endl; - } the}
PAT 1051. Pop Sequence (25)