链接:https://ac.nowcoder.com/acm/contest/551/D
来源:牛客网
在一行输出字典序最小的新字符串。
ASCII字符集包含 94 个可打印字符(0x21 - 0x7E),不包含空格。
题解:拿栈来维护就好了
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<stack> using namespace std; char s[100050]; int flag[100050]; stack<char>st; int vis[100050]; char ans[100050]; int main() { cin>>s; memset(flag,0,sizeof(flag)); memset(vis,0,sizeof(vis));//标记是否在栈内 int len=strlen(s); for(int i=0; i<len; i++) { flag[s[i]]++;//计算数量 } st.push(s[0]); vis[s[0]]=1;//标记 flag[s[0]]--; for(int i=1; i<len; i++) { flag[s[i]]--; if(vis[s[i]]) continue; while(!st.empty()&&st.top()>s[i]&&flag[st.top()]>0) { vis[st.top()]=0; st.pop(); } st.push(s[i]); vis[s[i]]=1; } int sum=0; while(!st.empty()) { ans[sum++]=st.top(); st.pop(); } for(int i=sum-1; i>=0; i--) cout<<ans[i]; cout<<endl; return 0; }
原文:https://www.cnblogs.com/yuanlinghao/p/10685881.html