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

https://codeforces.com/contest/1506/problem/G

用单调栈维护拿的字母即可

const int maxn = 1e3 + 7;

int n, t, m, cnt[300];
string s;

void solve() {
    cin >> t;
    while (t--) {
        memset(cnt, 0, sizeof cnt);
        cin >> s;
        for (auto c:s) cnt[c]++;
        string ans;
        for (auto c:s) {
            cnt[c]--;
            if (ans.find(c) != -1) continue;
            while (ans.size() && cnt[ans.back()] && ans.back() < c) ans.pop_back();
            ans.push_back(c);
        }
        cout << ans << endl;
    }
}

posted @ 2021-08-17 19:37  naymi  阅读(39)  评论(0)    收藏  举报