题目描述
给你一个字符串 s
,请你去除字符串中重复的字母,使得字符串中的字母都只出现一次,保证返回结果字典序最小。(要求不能打乱字符串中其他字符的相对位置)
输入格式
第一行输入一个字符串。
输出格式
返回一个字符串。
输入输出样例1
输入
"bcabc"
输出
"abc"
提示
0≤s.length≤104
#include <bits/stdc++.h>
using namespace std;
stack<char> st;
stack<char> ans;
map<char,int> mp;
map<char,int> ison;
int main(){
string s;
cin>>s;
for(int i=0;i<s.length();i++){
mp[s[i]]++;
ison[s[i]]=0;
}
for(int i=0;i<s.length();i++){
if(st.empty()||s[i]>=st.top()){
if(ison[s[i]]==0)
{
st.push(s[i]);
mp[s[i]]--;
ison[s[i]]=1;
}
else mp[s[i]]--;
}
else if(s[i]<st.top()&&ison[s[i]]==0){
while((!st.empty())&&mp[st.top()]>0){
ison[st.top()]=0;
st.pop();
}
st.push(s[i]);
mp[s[i]]--;
}
}
while(!st.empty()){
ans.push(st.top());
st.pop();
}
while(!ans.empty()){
cout<<ans.top();
ans.pop();
}
}