題目連結

好題。

AC Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;

// tree node stuff here...
#endif

static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();

class Solution
{
public:
    string removeDuplicateLetters(string s)
    {
        int cnt[26] = {0};
        for (auto i : s)
            cnt[i - 'a']++;

        bool in[26] = {false};
        stack<char> st;
        for (auto cc : s) {
            int c = cc - 'a';
            cnt[c]--;  // oops
            if (in[c]) // no need to insert again
                continue;

            // observation
            // if the current character is better than the top character in the stack
            // and
            // the top character still has occurrence after the current one:
            // pop it
            while (st.size() > 0 && cc < st.top() && cnt[st.top() - 'a'] > 0) {
                // cnt[st.top() - 'a']++; // don't do this
                in[st.top() - 'a'] = false;
                st.pop();
            }

            st.push(cc);
            in[c] = true;
        }

        // ans
        string ans = "";
        while (st.size() > 0) {
            ans += st.top();
            st.pop();
        }

        reverse(ans.begin(), ans.end());
        return ans;
    }
};

#ifdef LOCAL
int main()
{
    return 0;
}
#endif