线性结构3 Pop Sequence(PAT)

来源:互联网 发布:java含金量高的证书 编辑:程序博客网 时间:2024/06/08 13:49

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, ifM 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 2

Sample Output:

YESNONOYESNO

题意

第一行输入 M, N,T

M代表栈的容量,N为序列长度,T为测试组数


我们可以向容量为M的栈中按照1 , 2, 3..的顺序推入 1 ,2,  3.. , 可以任意弹出。问能否输出给定序列。

样例 5 6 4 3 7 2 1

推入 1 2 3 4 5 , 弹出 5, 此时 1 2 3 4

输入 6 ,推入 6, 此时 1 2 3 4 6 , 弹出6 ,此时 1 2 3 4

输入 4 ,弹出 4,此时 1 2 3

输入 3 ,弹出 3,此时 1 2

输入 7,推入 7 , 此时 1 2 7,弹出 7,此时 1 2

输入 2, 弹出 2 , 此时 1

输入1 ,弹出 1. 输出 YES!

直接模拟题意解决

#include<iostream>#include<cstdio>#include<stack>using namespace std;int main(){int n,m,t;scanf("%d%d%d",&n,&m,&t);stack<int>st;for(int i = 0;i<t;i++){int tmp = 1;bool False = 0;for(int j=0;j<m;j++){int input ;scanf("%d",&input);while(st.size() <= n && !False){ //满足条件就不断推入 1,2,3...if(st.empty() || st.top() != input)st.push(tmp++);else if(st.top() == input){ //如果栈顶元素与输入的一样了,就弹出。st.pop(); break;}if(st.size() > n){False =  1; break; //超过栈的范围了}}}if(!False)printf("YES\n");elseprintf("NO\n");while(!st.empty())st.pop();}return 0;}



1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 内六角螺丝的冒滑了怎么办 孔里的内六角滑了怎么办 十字螺丝刀的头卡在螺母里怎么办 内六角扳手断在螺丝孔里怎么办 六棱螺丝刀折进螺丝里了怎么办 内6棱螺丝拧花了怎么办 只有pe模式可以用键盘鼠标怎么办 电脑重做了系统连不上无线网怎么办 联想手机死机了怎么办不可拆卸电池 手机界面弹出后大小无法调整怎么办 方向盘打方向瑟瑟的吱吱响怎么办 三脚架提升杆螺丝滑丝了怎么办 花土里面有很多细长的螺丝怎么办 防盗门外边上边缝宽螺丝松了怎么办 填充墙与框架梁不对齐怎么办 亚轧滚珠丝杠螺帽超程了怎么办 在公司交的五险一金辞职了怎么办 铝合金的渣子整到眼睛里怎么办 铁锅手柄上的铆钉拧不动怎么办 喂完鱼鱼缸上边飘着一层油膜怎么办 铸铁管道横着排的结口漏水怎么办 缤智行李架免打孔螺丝款怎么办 前保险杠和叶子板缝隙大怎么办 新奥拓的大灯调节螺丝滑丝了怎么办 小米手环2计步不准怎么办 小米手环3计步不准怎么办 眼镜用洗发水洗了后模糊怎么办 雷朋近视镜眼镜腿折了怎么办 近视镜眼镜腿断了该怎么办 老师把学生的眼镜打坏了怎么办 生死狙击忘了密保改不了密码怎么办 我叫mt狂猎之弓怎么办 你能利用你的弱点打击你怎么办 研控两相混合伺服电机反转怎么办? 战地一要我升级显卡驱动怎么办 刺客信条兄弟会存档损坏了怎么办 电脑上所有软件和文件被删了怎么办 电脑显示文件已打开无法删除怎么办 金立手机桌面停止运行黑屏怎么办 苹果平板电脑玩游戏黑屏闪退怎么办 电脑开机时显示无法找到入口怎么办