G. Maximize the Remaining String
贪心,放置时,如果前面一个小比他小,并且后面还有,那么就把前面的删除
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 3;
#define int long long
string str;
char ans[N];
map<char,int>mp,vis;
signed main()
{int t;cin>>t;while (t--){int cnt = 0;cin>>str;int n = str.length();str = "1"+str;vis.clear(),mp.clear();memset(ans,0,sizeof ans);for (int i=1;i<=n;i++){mp[str[i]]=i;//记录最后一个在哪出现}for (int i=1;i<=n;i++){if (vis[str[i]]) continue;//如果前面出现了while (ans[cnt]<str[i]&&mp[ans[cnt]]>i)//如果他比这个字符小并且后面还有vis[ans[cnt--]] = 0;ans[++cnt] = str[i];vis[str[i]] = 1;}for (int i=1;i<=cnt;i++)cout<<ans[i];cout<<endl;}
}