栈(线性表2)

后缀表达式 

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() 
{
    stack<int>stk;
    char ch = '0';//初始化一个随便什么值
    int s = 0, x, y;
    while (ch != '@') {
        ch = getchar();
        switch (ch) {
            case '+': {
                x = stk.top();
                stk.pop();
                y = stk.top();
                stk.pop();
                stk.push(y + x);
                break;
            }
            case '-': {
                x = stk.top();
                stk.pop();
                y = stk.top();
                stk.pop();
                stk.push(y - x);
                break;
            }
            case '*': {
                x = stk.top();
                stk.pop();
                y = stk.top();
                stk.pop();
                stk.push(y * x);
                break;
            }
            case '/': {
                x = stk.top();
                stk.pop();
                y = stk.top();
                stk.pop();
                stk.push(y / x);
                break;
            }
            case '.': {
                stk.push(s);
                s = 0;
                break;
            }
            default: {
                s = s * 10 + ch - '0';
                break;
            }
        }
    }
    cout << stk.top() << endl;

    return 0;
}

注意字符串的处理

括号序列 

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() 
{
    stack<int>stk;
    bool ok[105];
    string s;
    cin >> s;
    for (int i = 0, k; i < s.length(); i++) {
        if (s[i] == ']') {
            if (stk.empty())continue;
            k = stk.top();
            if (s[k] == '[') {
                ok[k] = ok[i] = 1;
                stk.pop();
            }
        }
        else if (s[i] == ')') {
            if (stk.empty())continue;
            k = stk.top();
            if (s[k] == '(') {
                ok[k] = ok[i] = 1;
                stk.pop();
            }
        }
        else {
            stk.push(i);
        }
    }
    for (int i = 0; i < s.length(); i++) {
        if (ok[i])cout << s[i];
        else {
            if (s[i] == '(' || s[i] == ')')cout << "()";
            else cout << "[]";
        }
    }


    return 0;
}

害,题目看好几遍没理解,一看大佬题解就会了

【深基15.习9】验证栈序列 

#include <iostream>
#include <stack>
#include <string>

using namespace std;
const int N = 100005;
int main()
{
    int q;
    cin >> q;
    int a[N], b[N];
    for (int i = 0; i < q; i++) {
        int n;
        cin >> n;
        stack<int>stk;
        for (int j = 0; j < n; j++)cin >> a[j];
        for (int j = 0; j < n; j++)cin >> b[j];
        
        int k=0;
        for (int j = 0; j < n; j++) {
            stk.push(a[j]);
            while (stk.size() && stk.top() == b[k]) {
                stk.pop();
                k++;
            }
        }
        
        if (!stk.empty()) cout << "No" << endl;
        else cout << "Yes" << endl;
    }


    return 0;
}

注意:这道题不知道为什么一定要我用了数组的时候才能对,有大佬能解释一下吗