原题链接: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;
}