栈
Huangwenqi · · 算法·理论
:::align{center} :::
1. 使用数组模拟(摘自OI Wiki):
int st[N];
// 这里使用 st[0] (即 *st) 代表栈中元素数量,同时也是栈顶下标
// 压栈 :
st[++*st] = var1;
// 取栈顶 :
int u = st[*st];
// 弹栈 :注意越界问题, *st == 0 时不能继续弹出
if (*st) --*st;
// 清空栈
*st = 0;
:::info[封装]
template<typename T>
struct Stack{
int tp;
T st[maxn];
inline T &operator [](const int x){
return st[x];
}
inline bool empty()const{
return !tp;
}
inline void clear(){
tp=0;
}
inline int size(){
return tp;
}
inline void push(const T x){
st[++tp]=x;
}
inline bool pop(){
if(!tp) return 0;
tp--;
return 1;
}
inline T top(){
return st[tp];
}
};
:::
2.STL中的栈
- 头文件:<stack>
- 定义:stack<类型> 栈名;
- 函数:\
top()返回栈顶,\push(x):推入 x , \pop()弹出栈顶,\empty()返回是否为空,\size()返回元素数量
\color{black}\bold{B3614\ 【模板】栈}
思路
如题目描述。 :::info[代码]
#include<iostream>
#define int unsigned long long //不开 ull 见祖宗
using namespace std;
const int N=1e6+010;
int st[N],tot;
int n;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
tot=0; //多测清空
cin>>n;
string str;
while(n--){
cin>>str;
if(str=="push"){
int x;
cin>>x;
st[++tot]=x;
}
else if(str=="pop"){
if(!tot) cout<<"Empty\n";
else tot--;
}
else if(str=="query"){
if(!tot) cout<<"Anguei!\n";
else cout<<st[tot]<<'\n';
}
else cout<<tot<<'\n';
}
}
return 0;
}
:::
\color{black}\bold{P1449\ 后缀表达式}
思路
定义两个栈,输入数则存入数栈,输入符号则去除数栈最上方两个元素运算后再存回去。 :::info[代码]
#include<iostream>
#include<cstdio>
#include<stack>
#define int long long
using namespace std;
const int maxn=5e5+010;
stack<int> st;
int n,ans;
signed main(){
int x=0;
char ch=getchar();
while(ch!='@'){
if(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48);
else if(ch=='.'){
st.push(x);
x=0;
}
else if(ch=='+'){
int a,b;
a=st.top();
st.pop();
b=st.top();
st.pop();
st.push(a+b);
}
else if(ch=='-'){
int a,b;
a=st.top();
st.pop();
b=st.top();
st.pop();
st.push(b-a);
}
else if(ch=='*'){
int a,b;
a=st.top();
st.pop();
b=st.top();
st.pop();
st.push(b*a);
}
else if(ch=='/'){
int a,b;
a=st.top();
st.pop();
b=st.top();
st.pop();
st.push(b/a);
}
ch=getchar();
}
cout<<st.top();
return 0;
}
:::
\color{black}\bold{P1175\ 表达式的转换}
思路
输入:
- 对于数,直接插入到用来存后缀表达式链表最后;
- 对于
( ,入符号栈,然后不管; - 对于除
) 和 ^ 外的其他运算符,将运算级\leq 它的插入链表,然后将其入符号栈; - 对于 ^ 与上一条几乎相同,但把
\leq 改为< ; - 对于
) ,将它与它对应的( 之间的所有运算符插入链表。
计算:
- 从链表开头开始遍历,遇到操作符便将其与上两个数运算并删除,接着在同位置插入运算结果,然后输出,到无运算符(遍历到最后)为止。
:::info[代码]
#include<iostream> #include<map> using namespace std; const int N=1e2+010; struct Node{ int x; bool isnum; }s[N]; char st[N]; int tot,tp; map<char,int> mp; struct List{ Node x; int lst,nxt; }str[N]; int h=1,t=0; void In(){ char ch=getchar(),lst='+'; int x=0; while(ch^-1){ if('0'<=ch&&ch<='9') x=(x<<1)+(x<<3)+(ch^48); else{ if(isdigit(lst)){ s[++tot]={x,1}; x=0; } if(ch=='(') st[++tp]=ch; else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'){ while(tp&&st[tp]!='('&&(mp[st[tp]]>mp[ch]||ch^'^'&&mp[st[tp]]==mp[ch])) s[++tot]={st[tp--],0}; st[++tp]=ch; } else if(ch==')'){ while(st[tp]!='(') s[++tot]={st[tp--],0}; tp--; } } lst=ch; ch=getchar(); } while(tp) s[++tot]={st[tp--],0}; } void Change(){ for(int i=1;i<=tot;i++) str[++t]={s[i],t-1,t+1}; str[1].lst=-1; str[t].nxt=-1; } int qpow(int a,int b){ int ans=1; while(b){ if(b&1) ans*=a; a*=a,b>>=1; } return ans; } int main(){ mp['+']=mp['-']=1; mp['*']=mp['/']=2; mp['^']=3; mp['(']=mp[')']=4; In(); Change(); for(int i=h;~i;i=str[i].nxt){ if(str[i].x.isnum) cout<<str[i].x.x<<' '; else cout<<(char)str[i].x.x<<' '; } cout<<'\n'; for(int i=h;i!=-1;i=str[i].nxt){ if(!str[i].x.isnum){ char ch=str[i].x.x; int x=str[str[str[i].lst].lst].x.x, y=str[str[i].lst].x.x; if(ch=='+') str[str[str[i].lst].lst].x.x=x+y; if(ch=='-') str[str[str[i].lst].lst].x.x=x-y; if(ch=='*') str[str[str[i].lst].lst].x.x=x*y; if(ch=='/') str[str[str[i].lst].lst].x.x=x/y; if(ch=='^') str[str[str[i].lst].lst].x.x=qpow(x,y); str[str[str[i].lst].lst].nxt=str[i].nxt; str[str[i].nxt].lst=str[str[i].lst].lst; for(int j=h;~j;j=str[j].nxt){ if(str[j].x.isnum) cout<<str[j].x.x<<' '; else cout<<(char)str[j].x.x<<' '; } cout<<'\n'; } } return 0; }:::
\Huge \bold {单调栈}
插入元素
- 过程:对于将插入的元素
x ,判断其与栈顶元素的大小关系,若满足条件则插入,否则弹出栈顶再判断直至满足。 - 代码:
while(!st.empty()&&st.top()<x) st.pop(); st.push(x);\LARGE\bold{例题} \color{black}\bold{P5788\ 【模板】单调栈} 思路
对于每个在插入第
i 个元素时弹出的元素,记录其结果为i 。 :::info[代码]#include<iostream> #include<cstdio> #include<stack> using namespace std; const int maxn=3e6+010; int n,ans[maxn]; stack<pair<int,int> > st; int main(){ cin>>n; for(int i=1;i<=n;i++){ int x; cin>>x; while(st.size()&&st.top().first<x){ ans[st.top().second]=i; st.pop(); } st.push(make_pair(x,i)); } for(int i=1;i<=n;i++) cout<<ans[i]<<" "; return 0; }:::
\color{black}\bold{P1823\ [COI\ 2007]\ Patrik\ 音乐会的等待} 思路
维护一个
\operatorname{pair<int,int>} 的单调栈,\operatorname{first} 存高度,\operatorname{second} 存此高度人的个数(题目没说身高相等的不算一对),在弹出时让答案加上被弹出高度的人的个数,弹出完毕,判断要插入元素与顶部元素大小关系(记得判空):若比顶部元素高度小,答案加一再插入;若与顶部元素高度相等,加上顶部高度人数+1 (若只有一种高度(即栈中只有一个元素)则不+1 )再让顶部高度人数+1 ;否则直接插入。 :::info[代码]#include<iostream> #include<cstdio> #include<stack> #define int long long //不开 long long 见祖宗 using namespace std; const int maxn=5e5+010; template<typename T> struct Stack{ int tp; T st[maxn]; inline T &operator [](const int x){ return st[x]; } inline bool empty()const{ return !tp; } inline void clear(){ tp=0; } inline int size(){ return tp; } inline void push(const T x){ st[++tp]=x; } inline bool pop(){ if(!tp) return 0; tp--; return 1; } inline T top(){ return st[tp]; } }; Stack<pair<int,int> > st; int n,ans; signed main(){ cin>>n; for(int i=1,x;i<=n;i++){ cin>>x; while(!st.empty()&&st.top().first<x){ ans+=st.top().second; st.pop(); } if(st.top().first>x){ ans++; st.push({x,1}); } else if(st.top().first==x){ ans+=st.top().second+(bool)(st.size()^1); st[st.size()].second++; } else st.push({x,1}); } cout<<ans; return 0; }:::