单调栈:

创建访问标志map。

1.从后往前遍历字符串,遇到一个字符ch。

2.在map中查询是否存在ch。

1). 若不存在,ch入栈,入map。

2). 若存在:

  • 若ch小于栈顶字符,将栈中包含的ch弹出,ch入栈

  • 若ch大于栈顶字符,跳过.

循环1,2步。

void remove(string str){
    map<char,int> m;
    stack<char> st;
    for(int i = str.size()-1;i>=0;i--){
        char ch = str[i];
        //若没有碰到当前字符,直接入栈
        if(m.find(ch)==m.end()){
            //cout<<"push: "<<ch<<endl;
            st.push(ch);
            m.insert(pair<char,int>(str[i],1));
            continue;
        }
        //若当前字符以访问过,并且小于栈顶,弹出原来的该字符,新字符入栈
        if(ch<st.top()){
            stack<char> tmp;
            while(st.top()!=ch){
                tmp.push(st.top());
                st.pop();
            }st.pop();
            while(!tmp.empty()){
                st.push(tmp.top());
                tmp.pop();
            }st.push(ch);   
        }
    }
    while(!st.empty()){
        cout<<st.top();
        st.pop();
    }
}