时间限制
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 2Sample Output:
YES NO NO YES NO
自己写的毛病很多,终于改好了。最大的问题是每次测试完数据后需要清空栈
#include <iostream> #include <malloc.h> #define MAX 1001 using namespace std; typedef struct node { int data; struct node *next; }StackNode,*LinkStack; LinkStack createStack() { LinkStack ls; ls=NULL; return ls; } void pushStack(LinkStack &ls,int data) { StackNode *stNode; stNode = (StackNode*)malloc(sizeof(StackNode)); stNode->data=data; stNode->next=ls; ls=stNode; } int popStack(LinkStack &ls) { int data; StackNode *node; if(ls!=NULL) { data=ls->data; node=ls; ls=ls->next; free(node); return data; } return 0; } int topStackNode(LinkStack ls) { if(ls!=NULL) { return ls->data; } return 0; } void clearStack(LinkStack &ls) { while(topStackNode(ls)) popStack(ls); } int main() { int m,n,k,i,num; int stac[MAX]; LinkStack ls; cin>>m>>n>>k; while(k--) { ls=createStack(); num=0; int uu=0; for(i=0;i<n;i++) { cin>>stac[i]; if(stac[i]>uu) uu=stac[i]; } int tmp=0,sum_num=0; int flag=1; for(i=1;i<=n;i++) { if(uu>n) { flag=0; break; } flag=1; if(sum_num==m) { if(topStackNode(ls)==stac[tmp]) { popStack(ls); sum_num--; tmp++; } else { flag=0; break; } } pushStack(ls,i); //cout<<"push: "<<i<<endl; sum_num++; for(int j=tmp;j<n;j++) { if(topStackNode(ls)==0||topStackNode(ls)<stac[j]) break; else if(topStackNode(ls)==stac[j]) { //cout<<"pop: "<<topStackNode(ls)<<endl; popStack(ls); sum_num--; tmp++; }else if(topStackNode(ls)>stac[j]) { flag=0; break; } } if(flag==0) break; } if(flag==1&&tmp==n) cout<<"YES\n"; else cout<<"NO\n"; clearStack(ls); } return 0; }
建议参考:
#include<iostream> #include<stack> using namespace std; int main() { int M; //maximum capacity of the stack int N; //the length of push sequence int K; //the number of pop sequence to be checked cin >> M >> N >> K; int i, j; int input, temp; bool flag = true; stack<int> sta; for (i = 0; i < K; i++) { temp = 1; flag = true; for (j = 0; j < N; j++) { cin >> input; while (sta.size() <= M && flag) { if (sta.empty() || sta.top() != input) { sta.push(temp++); } else if (sta.top() == input) { sta.pop(); break; } } if (sta.size() > M) { flag = false; } } if (flag) cout << "YES" << endl; else cout << "NO" << endl; while (!sta.empty()) sta.pop(); } return 0; }