当前位置:网站首页>Discussion and legitimacy of the order of entering and leaving the stack
Discussion and legitimacy of the order of entering and leaving the stack
2022-07-24 14:50:00 【Liyueyue classmate】
The characteristic of stack is first in first out . However, it should be noted that this feature is only established under normal conditions and normal operation , What is normal and what is abnormal ?

The so-called normal operation : Pressing all data into the stack in strict order and then pressing it out of the stack in turn is normal routine operation . For example, we bring a set of data :1,2,3,4,5; After they are put into the stack, the order of getting out of the stack is naturally 5,4,3,2,1.
Then what will happen 4,5,3,2,1 Out of stack order ? Most people who are exposed to this kind of situation for the first time may think there is a problem , Problems like mistakes , Then you have to think , Is it strict input stack operation ? In other words, whether it is in order out of the stack into the stack ? The problem is solved !
analysis : Let's put... First in the normal order 1,2,3,4 Push to stack , Then the key steps came , At this time, put 4 Press out stack ,4 Is the first one out of the stack ; And then put 5 Push , Finally, stack in order , Then the final stack order we get is 4,5,3,2,1.
Do an exercise :
The stack sequence of a stack is a,b,c,d,e Then the impossible output sequence of the stack is :()
A edcba B decba C dceab D abcde
I put the answer analysis at the end .
Look at the following question
Stack and queue 7 of data structure experiment : Stack sequence determination
Description
Give an initial stack sequence , The second order is the stacking order of elements , The top element of the stack can be out of the stack at any time , Each element can only be put on the stack . Enter a stack sequence , Then enter multiple sequences in turn , Please judge whether these sequences are legal stack sequences for the given stack sequence .
For example, the sequence 1,2,3,4,5 Is the push order of a stack , Sequence 4,5,3,2,1 It is a stack out sequence corresponding to the stack pressing sequence , but 4,3,5,1,2 It can't be the out of stack sequence of the sequence . Suppose that all the Numbers pushed are not equal .
Input
Enter the integer in the first line n(1<=n<=10000), Represents the length of the sequence .
Second line input n It's an integer , Indicates the pressing order of the stack .
In the third line, enter the integer t(1<=t<=10).
Input in sequence t That's ok , Each row n It's an integer , Represents each out of stack sequence to be judged .
Output
Output one line for each test case , If the initial stack sequence can be obtained, the stack sequence , The output yes, Otherwise output no.
Sample
Input
5
1 2 3 4 5
2
4 5 3 2 1
4 3 5 1 2Output
yes
noCode :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,a[10001],b[10001];
cin>>n;
for(i=0; i<n; i++)
cin>>a[i];
cin>>m;
while(m--)
{
stack<int> st;
for(i=0; i<n; i++)
cin>>b[i];
int key_a=0,key_b=0;
while(key_b<n)
{
if(st.empty())
st.push(a[key_a++]);
else
{
if(st.top()!=b[key_b])
{
if(key_a<n)
st.push(a[key_a++]);
else
break;
}
else
{
st.pop();
key_b++;
}
}
}
if(st.empty())
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}C
Analyze the answers one by one ,A Options are the result of the normal order in and out of the stack as we mentioned above ;B We have also analyzed the options, which are the examples at the beginning of the article ;D The result of the option is to press in one and pop up one ;C Options , Let's analyze ab The order after , Out of the stack for dce We can understand it as pressing d Pop up after d, In the pop-up c, Then press e eject e, No problem, we can guarantee cde Stack order of , that b stay a Then put on the stack , It's impossible to compare a Post stack so error .
边栏推荐
- [matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes
- 北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%
- PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
- Rest style
- 关于构建网络安全知识库方向相关知识的学习和思考
- Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
- Preparation of mobile end test cases
- Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布
- 老虎口瀑布:铜梁版小壶口瀑布
- AtCoder Beginner Contest 261 F // 树状数组
猜你喜欢

kali简洁转换语言方法(图解)

Moving the mouse into select options will trigger the mouseleave event processing scheme
![Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布](/img/fd/c7bff1ce199e8b600761d77828c674.png)
Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布

IEEE Transaction期刊模板使用注意事项

Grpc middleware implements grpc call retry

深入浅出边缘云 | 2. 架构

Learning rate adjustment strategy in deep learning (1)

Data analysis and mining 1
![[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes](/img/4d/b0ba599a732d1390c5eeb1aea6e83c.png)
[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes

Overview of dobesie wavelet (DB wavelet function) in wavelet transform
随机推荐
Grpc middleware implements grpc call retry
【MATLAB】MATLAB画图系列二 1.元胞与数组转化 2.属性元胞 3.删除nan值 4.合并多fig为同一fig 5.合并多fig至同一axes
Property datasource is required exception handling [idea]
佣金哪家券商最低,我要开户,手机上开户安不安全
Video game design report template and resources over the years
Learning and thinking about the relevant knowledge in the direction of building network security knowledge base
VSCode如何调试Nodejs
Not configured in app.json (uni releases wechat applet)
Production environment tidb cluster capacity reduction tikv operation steps
Learning rate adjustment strategy in deep learning (1)
Meaning of 7 parameters of thread pool
TypeError: Cannot read property ‘make‘ of undefined
Performance test - analyze requirements
Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
exchange
打假Yolov7的精度,不是所有的论文都是真实可信
[oauth2] III. interpretation of oauth2 configuration
Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
Automated penetration scanning tool
IEEE Transaction期刊模板使用注意事项