栈和队列入门题单
栈和队列
知识点学习
1.后进先出的栈(stack)
//tt表示栈顶
int stk[N],tt=0;
//向栈顶插入一个数
stk[++tt]=x;
//从栈顶弹出一个数
tt--;
//得到栈顶的值
stk[tt];
//判断栈是否为空
if(tt>0){}
2.先进先出的队列(queue)
- 普通队列
//hh表示队头,tt表示队尾
int q[N],hh=0,tt=-1;
//向队尾插入一个值
q[++tt]=x;
//从队头弹出一个数
hh++;
//得到队头的值
q[hh];
//判断队列是否为空
if(hh<=tt){}
- 循环队列
//hh表示队头,tt表示队尾
int q[N],hh=0,tt=0;
//向队尾插入一个值
q[++tt]=x;
if(tt==N),tt=0;
//从队头弹出一个数
hh++;
if(hh==N),hh=0;
//得到队头的值
q[hh];
//判断队列是否为空
if(hh!=tt){}
入门练习题目
有关栈的题目
1.栈
题目描述
提交代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,top,s[N];
char str[11];
int main(){
scanf("%d",&n);
top=0;
for(int i=1;i<=n;i++){
scanf("%s",str);
if(str[2]=='s'){
int x;scanf("%d",&x);
s[++top]=x;
}else{
if(str[0]=='p'){
top--;
}else{
int k;scanf("%d",&k);
printf("%d\n",s[top+1-k]);
}
}
}
return 0;
}
2.出栈序列判断
题目描述
提交代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,top,s[N];
int main(){
scanf("%d",&n);
top=0;
int l=0;
//双指针算法
for(int i=1;i<=n;i++){
int x;scanf("%d",&x);
if(s[top]!=x){
for(int j=l+1;j<=x;j++){
s[++top]=j;
printf("push %d\n",j);
}
l=x;
}
printf("pop\n");
top--;
}
return 0;
}
3.括号序列
题目描述
提交代码
#include<cstring>
#include<cstdio>
const int N=1e5+10;
int n,top,s[N];
char str[N];
int main(){
scanf("%s",str);
top = 0;
bool ok = 1;
int length=strlen(str);
for(int i=0;i<length && ok;i++){
if(str[i]=='('||str[i]=='{'||str[i]=='[') s[++top]=str[i];
else{
if(!top) ok = false;
else{
if(str[i]==')'){
if(s[top]=='(') top--;
else ok=false;
}else if(str[i]==']'){
if(s[top]=='[') top--;
else ok = false;
}else{
if(s[top]=='{') top--;
else ok=false;
}
}
}
}
if(top) ok = false;
if(ok) puts("true");
else puts("false");
return 0;
}
4.字符串处理
题目描述
给定一个长度为n的字符串s,字符串由小写字母a..z组成。
小明来对这个字符串进行操作,他会从头到尾检查这个字符串,如果发现有两个相同的字母并排在一起,就会把这两个字符都删掉。小明会重复这个操作,直到没有相邻的相同字母。
你需要给出处理完成的字符串。
-
输入格式
第一行一个整数n。
接下来一行一个长度为n的字符串s。
-
输出格式
输出最后处理完成的字符串,有可能是空串。 -
样例输入1
9
acccabddb
- 样例输出1
aca
- 样例输入2
5
ababa
- 样例输出2
ababa
- 数据规模
对于所有数据,保证n≤100000。
提交代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,top,s[N];
char str[N];
int main(){
scanf("%d%s",&n,str);
top = 0;
for(int i=0;i<n;i++){
if(s[top]!=str[i]) s[++top] = str[i];
else top--;
}
for(int i=1;i<=top;i++) printf("%c",s[i]);;
return 0;
}
有关队列的题目
1.队列
题目描述
提交代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int m,q[N],front=0,rear=0;
char str[1011];
int main(){
scanf("%d",&m);
for(int i=1;i<=m;i++) {
scanf("%s",str);
if(str[2]=='s'){
int x;scanf("%d",&x);
q[++rear]=x;
}else{
if(str[0]=='p'){
++front;
}
else{
int x;scanf("%d",&x);
printf("%d\n",q[front+x]);
}
}
}
return 0;
}
2.队列练习
题目描述
提交代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int x,k,q[N],front=0,rear=0;
int main(){
scanf("%d%d",&x,&k);
q[rear]=x;
for(int i=1;i<=k;i++){
q[++rear]=q[front]*2;
q[++rear]=q[front]*2+1;
printf("%d\n",q[front]);
++front;
}
return 0;
}
3.约瑟夫问题
题目描述
提交代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,m,q[N],front=1,rear=0;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) q[++rear]=i;
int c=0;
for(;rear>=front;){
c++;
if(c==m){
printf("%d ",q[front]);
++front;
c=0;
}
else {
q[++rear]=q[front];
++front;
}
}
return 0;
}