PAT 鐢茬骇 1051 Pop Sequence (25 鍒?锛堟ā鎷熸爤,杈冪畝鍗曪級
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 鐢茬骇 1051 Pop Sequence (25 鍒?锛堟ā鎷熸爤,杈冪畝鍗曪級相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/ecif' title='ecif'>ecif
gre cstring -o star length 瀹归噺 follow mes
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N 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 M is 5 and N 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): M (the maximum capacity of the stack), N (the length 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 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
棰樻剰锛?/span>
缁欏畾涓€涓?span style="color: #ff0000;">鏈夊浐瀹氬閲?/strong>鐨?/span>鏍堬紝1,2,...,n鏄叆鏍堝簭鍒楋紝鍏冪礌鍑烘爤椤哄簭闅忔剰锛岀幇缁欏畾鍑烘爤椤哄簭(e.g.1~n鐨勪竴涓帓鍒?锛岄棶杩欎釜鍑烘爤椤哄簭鏄惁鍚堢悊锛屽悎鐞嗚緭鍑?YES"锛屽惁鍒欒緭鍑?NO"銆?/span>
棰樿В锛?/span>
寮€涓€涓槦鍒楋紝寮€涓€涓爤锛岃緭鍏ヤ竴涓暟锛屽氨闃熷垪涓繖涓暟鍙婁箣鍓嶇殑鏁版斁鍏ユ爤涓紝鏀惧叆涓嶈兘瓒呰繃瀹归噺銆傜劧鍚庣湅鏍堥《鍏冪礌鏄笉鏄繖涓暟锛屾槸灏变笅涓€涓紝涓嶆槸灏辨爣璁癗O銆?/span>
AC浠g爜锛?/span>
#include<iostream> #include<stack> #include<queue> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<cstring> using namespace std; int n,m,k; stack<int>s; queue<int>q; int main(){ cin>>n>>m>>k; while(k--){ while(!s.empty()) s.pop(); while(!q.empty()) q.pop(); int f=1; for(int i=1;i<=m;i++) q.push(i); for(int i=1;i<=m;i++){ int x; cin>>x; if(s.empty()||s.top()!=x){ while(!q.empty()){ if(s.size()<n) { //cout<<"鎶?<<q.front()<<"鏀炬爤"<<endl; s.push(q.front()); q.pop(); } else break; if(s.top()==x) break; } } if(s.top()==x){ s.pop(); //cout<<x<<"韪㈠嚭鏍?<<endl; continue; } f=0; } if(f) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
以上是关于PAT 鐢茬骇 1051 Pop Sequence (25 鍒?锛堟ā鎷熸爤,杈冪畝鍗曪級的主要内容,如果未能解决你的问题,请参考以下文章