Codeforces Round #710 (Div. 3) G. Maximize the Remaining String 单调栈

原题链接:https://codeforces.ml/contest/1506/problem/G

题意

有一个字符串,你可以删掉一些字符,要求最开始每个字符都至少剩一个且字典序最大

分析

首先满足单调性,其次要求每个字符都剩至少一个,可以用单调栈解决,记录后面还剩几个该字符,如果没有,就留下,如果还有,就可以被替换

Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <bitset>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>

using namespace std;
#define fi first
#define se second
#define re register
typedef long long ll;
typedef pair<ll, ll> PII;
typedef unsigned long long ull;
const int N = 3e5 + 10, M = 1e6 + 5, INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int stk[N], top;
void solve() {
    int T; cin >> T; while (T--) {
        string s; cin >> s;
        vector<int> cnt(26), vis(26); top = 0;
        for (int i = 0; i < s.size(); i++) cnt[s[i]-'a']++;
        for (int i = 0; i < s.size(); i++) {
            if (vis[s[i]-'a']) {
                cnt[s[i]-'a']--;
                continue;
            }
            while (top && s[stk[top]] < s[i] && cnt[s[stk[top]]-'a']) {
                vis[s[stk[top]]-'a'] = 0;
                //cnt[s[stk[top]]-'a']--;
                top--;
            }
            stk[++top] = i;
            vis[s[stk[top]]-'a'] = 1;
            cnt[s[stk[top]]-'a']--;
        }
        for (int i = 1; i <= top; i++) cout << s[stk[i]];
        cout << endl;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值