1051 Pop Sequence (25分)ios
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.面試
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.數組
For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.數據結構
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~ n 一次入棧,在入棧的過程當中若是入棧的元素剛好等於出棧的序列當前等待出棧的元素,則讓其出棧,同時把棧序列當前等待的元素位置標記後移一位。此時,只要棧頂元素仍然等於出棧序列當前等待元素,則持續出棧。dom
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stack> using namespace std; const int maxn = 1010; stack<int> S; int arr[maxn]; int main() { int m, n, T; scanf("%d%d%d", &m, &n, &T); while (T--) { while (!S.empty()) S.pop(); for (int i = 1; i <= n; i++) { scanf("%d", &arr[i]); } int current = 1; bool flag = true; for (int i = 1; i <= n; i++) { S.push(i); if (S.size() > m) { flag = false; break; } while (!S.empty() && S.top() == arr[current]) { S.pop(); current++; } } if (S.empty() == true && flag == true) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }
難度中等11spa
棧排序。 編寫程序,對棧進行排序使最小元素位於棧頂。最多隻能使用一個其餘的臨時棧存放數據,但不得將元素複製到別的數據結構(如數組)中。該棧支持以下操做:push
、pop
、peek
和 isEmpty
。當棧爲空時,peek
返回 -1。code
示例1:排序
輸入: ["SortedStack", "push", "push", "peek", "pop", "peek"] [[], [1], [2], [], [], []] 輸出: [null,null,null,1,null,2]
示例2:ci
輸入: ["SortedStack", "pop", "pop", "push", "pop", "isEmpty"] [[], [], [], [1], [], []] 輸出: [null,null,null,null,null,true]
說明:leetcode
要求不能用別的,只能用棧,那就使用一個主棧和一個輔助棧。
先給主棧壓棧,若是主棧裏面已經有元素了就要比大小。若是原來的元素大,則直接壓棧。不然把原來的元素壓到輔助棧中,比較下一個,以此類推直到比到比新元素大舊元素,而後壓棧,再把輔助棧中的元素放回來。持續以上步驟,直至結束。
//AC程序1 class SortedStack { private: stack<int>s1;//原棧爲升序 stack<int>s2;//輔助棧爲降序 public: SortedStack() { } void push(int val) { while(!s2.empty()&&s2.top()>val){//輔助棧中存在比val大的值 s1.push(s2.top()); s2.pop(); } while(!s1.empty()&&s1.top()<val){//原棧中有比val小的值 s2.push(s1.top()); s1.pop(); } s1.push(val); } void pop() { while(!s2.empty()){//清空輔助棧 s1.push(s2.top()); s2.pop(); } if(!s1.empty()) s1.pop(); } int peek() { while(!s2.empty()){//清空輔助棧 s1.push(s2.top()); s2.pop(); } if(!s1.empty()) return s1.top(); else return -1; } bool isEmpty() { return s1.empty()&&s2.empty(); } }; //AC程序2 class SortedStack { public: stack<int> data,as; SortedStack() { } void push(int val) { while(!data.empty()&&data.top()<val){ as.push(data.top()); data.pop(); } data.push(val); while(!as.empty()){ data.push(as.top()); as.pop(); } } void pop() { if(data.size()) data.pop(); } int peek() { if(data.size()) return data.top(); return -1; } bool isEmpty() { return data.empty(); } };