栈

· · 算法·理论

\Huge \bold 栈

:::align{center} :::

\LARGE\bold{定义} 栈的修改与访问是按照后进先出的原则进行的,\\ 因此栈通常被称为是后进先出(last\ in\ first\ out)表,简称\ LIFO\ 表。\\ \kern{250px}——\bold{OI\ Wiki} \LARGE\bold{实现}

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中的栈

\LARGE\bold{例题}

\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\ 表达式的转换}

思路

输入:

计算:

\LARGE\bold{定义} 单调栈即满足单调性的栈结构。\\ \kern{145px}——\bold{OI\ Wiki} \LARGE\bold{实现}

插入元素

将一个元素插入单调栈时,\\ 为了维护栈的单调性,\\ 需要在保证将该元素插入到栈顶后整个栈满足单调性的前提下\\ 弹出最少的元素。\\ \kern{145px}——\bold{OI\ Wiki}