牛客刷题-Day25

牛客刷题-Day25

今日刷题:\(A-E\)

A 栈和排序

fa62f1f449b1c62eae8339ec47f7050e

解题思路

题目要求尽可能按照字典序最大的顺序进行出栈操作,则设置变量 \(tmp\) 表示当前期望出栈的元素:

  • 当入栈元素等于 \(tmp\) 时,直接出栈;
  • 当入栈元素不等于 \(tmp\) 时,仅入栈;

最终若栈中还有元素,依次出栈即可得到最终的出栈序列。这里的出栈判断实际是贪心策略,每次尽可能让当前最大的元素(即等于 \(tmp\))出栈。

C++ 代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;

int n;
int stk[N], tt;

int main() {
    scanf("%d", &n);
    int tmp = n;
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        if (x == tmp) {
            tmp--;
            printf("%d ", x);
        } else {
            stk[++tt] = x;
        }
    }
    while (tt)
        printf("%d ", stk[tt--]);
    return 0;
}

B 牛牛与后缀表达式

aae15581-82d0-4be2-a796-ac9b99148371

解题思路

这里每次匹配到运算符,则将栈顶的两个元素出栈,进行运算,将结果入栈,最终栈顶元素为最终结果。

注意:下面的代码未处理元素为负数的情况,但可以通过牛客的测评,实际还是需要考虑全面。

C++ 代码

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定一个后缀表达式,返回它的结果
     * @param str string字符串 
     * @return long长整型
     */
    long long legalExp(string str) {
        long long tmp = 0;
        for (int i = 0; str[i]; i++) {
            if (str[i] >= '0' && str[i] <= '9') {
                tmp = tmp * 10 + str[i] - '0';
            } else if (str[i] == '#') {
                stk.push(tmp);
                tmp = 0;
            } else {
                long long b = stk.top();
                stk.pop();
                long long a = stk.top();
                stk.pop();
                if (str[i] == '+') {
                    stk.push(a + b);
                } else if (str[i] == '-') {
                    stk.push(a - b);
                } else {
                    stk.push(a * b);
                }
                // printf("%lld\n", stk.top());
            }
        }
        return stk.top();
    }
private:
    stack<long long> stk;
};

C 好串

9a851172-3a27-4897-a886-9a577b1fd286

解题思路

根据题意,每次操作,都是插入字符串 ab,则 ab 每次插入都是成对存在的且 a 在前方,因此通过栈进行匹配,最终栈为空,则说明所有的 ab 都匹配成功,该字符串为 Good;否则,为 Bad

C++ 代码

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;

string s;
stack<char> stk;

int main() {
    cin >> s;
    for (int i = 0; s[i]; i++) {
        if (s[i] == 'b' && (!stk.empty() && stk.top() == 'a')) {
            stk.pop();
        } else {
            stk.push(s[i]);
        }
    }
    if (!stk.empty())
        puts("Bad");
    else
        puts("Good");
    return 0;
}

D Rails

ab02d960-de7f-4c74-9cac-22b13361c433

解题思路

按照 \(1~n\) 的顺序入栈,给出一个出栈序列,判断出栈序列是否合法。

C++ 代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;

int n, a[N];
int stk[N], tt;

bool end() {
    cin >> n;
    if (n) return false;
    return true;
}

int main() {
    while (!end()) {
        cin >> a[0];
        while (a[0]) {
            tt = 0;
            a[1] = a[0];
            for (int i = 2; i <= n; i++)
                cin >> a[i];
            for (int i = 1, j = 1; i <= n; i++) {
                stk[++tt] = i;
                while (tt && stk[tt] == a[j]) {
                    tt--;
                    j++;
                }
            }
            if (tt)
                puts("No");
            else
                puts("Yes");
            cin >> a[0];
            if (!a[0])
                cout << endl;
        }
    }
    return 0;
}

E 吐泡泡

705997e7-3cdb-497c-bec9-b9c90cd878a6

解题思路

分情况讨论:

  1. 栈顶为 o 且入栈元素为 o,则栈顶元素出栈与入栈元素合成 O,此时,若栈顶还有元素且为 O,则栈顶元素出栈,否则合成的 O 入栈;
  2. 栈顶为 O 且入栈元素为 O,则栈顶元素出栈;
  3. 其余情况,元素入栈。

C++ 代码

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;

int T;
int stk[N], tt;
string s;

int main() {
    cin >> T;
    while (T--) {
        tt = 0;
        cin >> s;
        for (int i = 0; s[i]; i++) {
            if (s[i] == 'o' && (tt && stk[tt] == ('o' - 'a'))) {
                tt--;
                if (tt && stk[tt] == ('O' - 'a'))
                    tt--;
                else
                    stk[++tt] = 'O' - 'a';
            } else if (s[i] == 'O' && (tt && stk[tt] == ('O' - 'a'))) {
                tt--;
            } else {
                stk[++tt] = s[i] - 'a';
            }
        }
        for (int i = 1; i <= tt; i++)
            cout << (char) (stk[i] + 'a');
        cout << endl;
    }
    return 0;
}
posted @ 2025-12-02 09:37  Cocoicobird  阅读(7)  评论(0)    收藏  举报