博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
02-线性结构4 Pop Sequence
阅读量:6067 次
发布时间:2019-06-20

本文共 2547 字,大约阅读时间需要 8 分钟。

02-线性结构4 Pop Sequence(25 分)

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

#include 
#include
using namespace std;typedef struct node{ int data; struct node *next;}linkStack;linkStack *createStack(){ linkStack *s; s=(linkStack *)malloc(sizeof(linkStack)); s->next=NULL; return s;}int isEmpty(linkStack * s){ return s->next==NULL;}void push(int item,linkStack *s){ struct node *temCell; temCell=(linkStack *)malloc(sizeof(linkStack)); temCell->data=item; temCell->next=s->next; s->next=temCell;}int pop(linkStack *s){ if(isEmpty(s)) return NULL; else{ int data; linkStack *first; first=(linkStack *)malloc(sizeof(linkStack)); first=s->next; s->next=first->next; data=first->data; free(first); return data; }}int main(){ int m,n,k,num; cin>>m>>n>>k; int seques[n+1]={
1}; for(int i=0;i
>num; if(!isEmpty(s) && num
next->data){ isOrno=0; break; } else{ for(int i=1;i<=num;i++){ if(seques[i]){ push(i,s); seques[i]=0; cnt++; } } } if(cnt>m){ isOrno=0; break; } pop(s); seques[num]=0; cnt--; } if(isOrno) cout<<"YES"<
>num; j++; } } return 0;}

转载于:https://www.cnblogs.com/JingwangLi/p/10202834.html

你可能感兴趣的文章
loadrunner java协议脚本要点
查看>>
Windows Dos 打开服务命令(转)
查看>>
read,write,accept,connect 超时封装
查看>>
Redis主从配置及HA方案
查看>>
统计字符次数
查看>>
[国家集训队]Crash的数字表格【莫比乌斯反演】
查看>>
Openmeeting 网页打开缓慢,视频卡的一个解决方法
查看>>
倾斜摄影技术在城市规划行业中扮演着什么样的角色?
查看>>
Linux - route & traceroute & ip
查看>>
Elementary methods in number theory exercise 1.4.37 $1+\frac{1}{2}+\frac{1}{3}+\cdots+\frac{1}{n...
查看>>
不支持关键字:metadata
查看>>
论文笔记:Ten years of pedestrian detection, what have we learned?
查看>>
node.js-3
查看>>
ARTS打卡计划第6周-REVIEW-超越编码的避免项目失败的软技能
查看>>
javascript on方法
查看>>
初始if..else 条件语句
查看>>
python FileError
查看>>
《深入理解计算机系统》第一章学习笔记
查看>>
Rocket - util - MultiWidthFifo
查看>>
jQuery fullPage 全屏滚动
查看>>