模拟栈

太ez

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int m;
    cin>>m;
    vector<int> stack(m,0);
    int sp = 0;int x=0;
    string op;
    while (m -- ){
        cin >> op;
        if(op=="push"){
            cin >> x;
            stack[++sp] = x;
        }
        if(op=="pop"){
            if(sp){
                sp--;
            }
        }
        if(op=="empty"){
            if(sp){
                cout << "NO" <<endl;
            }else{
                cout << "YES"<<endl;
            }
        }
        if(op=="query"){
            if(sp){
                cout << stack[sp] << endl;
            }
        }
    }
    
    return 0;
}

表达式求值

这个有点难度说实话

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>
//需要复习
using namespace std;
unordered_map<char,int> pr={{'+',1},{'-',1},{'*',2},{'/',2}};
stack<int> num;
stack<char> opt;
/*
口诀
左入右算左
符优算完入
数入算完结
*/
void eval(){
    int b = num.top();num.pop();//这里注意是先弹出b
    int a = num.top();num.pop();
    char o = opt.top();opt.pop();
    switch(o){
        case '+':num.push(a+b);break;
        case '-':num.push(a-b);break;
        case '*':num.push(a*b);break;
        case '/':num.push(a/b);break;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string op;
    int x=0;
    cin >> op;
    for (int i = 0; i < op.size(); i ++ ){
        if(isdigit(op[i])){//如果是数字就读取数字,压入数字栈
            x=op[i++]-'0';
            while(op[i]>='0'&&op[i]<='9')x = x * 10 + op[i++] - '0';
            i--;
            num.push(x);
        }else if(op[i]=='(') opt.push('(');//如果是左括号,压入栈
        else if(op[i]==')'){//如果是右括号,一直计算到左括号才停止然后弹出左括号
            while(opt.size()&&opt.top()!='(') eval();
            opt.pop();
        }else{//如果是运算符就运算,如果栈顶的优先级大先运算栈顶
            while(opt.size()&&pr[opt.top()]>=pr[op[i]]){
                eval();
            }
            opt.push(op[i]);
        }
    }
    while(opt.size())eval();
    
    cout<<num.top();
    
    return 0;
}   

感觉死记硬背记住了,自己又写了一次,但是优先级的值写错了应该是:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <stack>
using namespace std;

unordered_map<char,int> pr={{'+',1},{'-',1},{'*',2},{'/',2}};
stack<int> num;
stack<char> opt;

void eval(){
    int b = num.top();num.pop();
    int a = num.top();num.pop();
    char o = opt.top();opt.pop();
    switch(o){
        case '+':num.push(a+b);break;
        case '-':num.push(a-b);break;
        case '*':num.push(a*b);break;
        case '/':num.push(a/b);break;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string op;
    cin >> op;
    int x;
    char c;
    for (int i = 0; i < op.size(); i ++ ){
        c = op[i];
        if(isdigit(c)){
            x = op[i++] - '0';
            while(isdigit(op[i])){
                x = x*10 + op[i++] - '0';
            }
            i--;
            num.push(x);
        }
        else if(c=='(') opt.push(c);
        else if(c==')'){
            while(opt.size()&&opt.top()!='(')eval();
            opt.pop();
        }
        else{
            while(opt.size()&&pr[opt.top()]>=pr[c])eval();
            opt.push(c);
        }
    }
    while(opt.size())eval();
    cout << num.top();
}

单调栈

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

struct Node{
    int num,pos;
};
//完全独立做出来
int main()
{
    int n;
    cin >> n;
    vector<int> A(n+10);
    for (int i = 0; i < n; i ++ )cin>>A[i];
    stack<int> S;
    
    cout << "-1 ";
    S.push(A[0]);
    for (int i = 1; i < n; i ++ ){
        while(S.size()&&S.top()>=A[i])S.pop();//把比我大的都弹出
        if(S.empty())cout << -1 << " ";//空了说明没有比我小的了
        else cout << S.top() << " ";//不空那么顶上的就是比我小的
        if(S.empty()||A[i]<A[i+1]) S.push(A[i]);//最后如果空了或者我比后面的小,就把我加上
    }
       
    return 0;
}

更新:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

//完全独立做出来
int main()
{
    int n;
    cin >> n;
    vector<int> A(n+10);
    for (int i = 0; i < n; i ++ )cin>>A[i];
    stack<int> S;
    
    cout << "-1 ";
    S.push(A[0]);
    for (int i = 1; i < n; i ++ ){
        while(S.size()&&S.top()>=A[i])S.pop();//把比我大的都弹出
        if(S.empty())cout << -1 << " ";//空了说明没有比我小的了
        else cout << S.top() << " ";//不空那么顶上的就是比我小的
        S.push(A[i]);//加上
    }
       
    return 0;
}
posted @ 2025-07-25 15:17  .N1nEmAn  阅读(7)  评论(0)    收藏  举报