02-线性结构4. Pop Sequence (25)
来源:互联网 发布:淘宝美工店铺装修教程 编辑:程序博客网 时间:2024/06/06 02:03
时间限制
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 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 2Sample Output:
YESNONOYESNO
自己写的毛病很多,终于改好了。最大的问题是每次测试完数据后需要清空栈
#include <iostream>#include <malloc.h>#define MAX 1001using 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;}
- 02-线性结构4. Pop Sequence (25)
- 02-线性结构4. Pop Sequence (25)
- 02-线性结构4. Pop Sequence(25)
- 02-线性结构4. Pop Sequence (25)
- 线性结构4. Pop Sequence(25)
- 02-线性结构3. Pop Sequence (25)
- 02-线性结构3. Pop Sequence (25)
- 02-线性结构3. Pop Sequence (25)
- PAT 数据结构 02-线性结构4. Pop Sequence (25)
- PAT 02-线性结构3. Pop Sequence (25)
- 02-线性结构3 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分)
- 02-线性结构4 Pop Sequence (25分)
- unity3D创建英雄选择栏
- 使用UltraISO制作U盘启动盘
- java基础--网络编程
- java笔记之一
- 深圳之行---错过
- 02-线性结构4. Pop Sequence (25)
- 递归函数的理解
- x86 gcc 里绝对值优化求法
- 各种编码UNICODE、UTF-8、ANSI、ASCII、GB2312、GBK详解
- Netbeans打开包含中文文件时提示错误
- java笔记之二
- java 实现生成excel表头
- 第四章(小型对象分配技术:smart-object allocation)
- bzoj3651&3081: 网络通信