1506G
题意:给出一个字符串,删掉其中某些出现了大于等于两次的字符,并最大化每一个字符只出现一次的字符串
思路:从一个空串开始添加字符。因为要求的是最大字符串,因此我们最大化每一次添加的字符。但是什么样的字符是能够添加的呢?
答案是这个字符第一个出现的位置的后面包含了所有剩余的字符,否则添加了这个字符可能会使某些字符无法添加。
#include <bits/stdc++.h>
using namespace std;
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define DEBUG(var) cout << #var << "=" << var << '\n'
#define REP(i, a, n) for (int i = a; i <= n; ++i)
#define PER(i, n, a) for (int i = n; i >= a; --i)
#define LL int64_t
template<typename T>void Read(T &x){x=0;char ch=getchar();LL f=1;while(!isdigit(ch)){if(ch=='-')f*=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}x*=f;}
template<typename T>int64_t __lcm(T a, T b){return a/__gcd(a,b)*b;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t Rand(LL a,LL b) {return a + rng() % (b-a+1);}
inline int64_t QuickPow(LL base,LL n,LL Mod=0){LL ret(1);while(n){if(n&1){ret*=base;if(Mod)ret%=Mod;}base*=base;if(Mod)base%=Mod;n>>=1;}return Mod?ret%Mod:ret;}
inline int64_t Inv(LL x,LL p){return QuickPow(x,p-2,p);}
inline void gettime(){time_t rawtime;struct tm *ptminfo;time(&rawtime);ptminfo = localtime(&rawtime);printf("Current time: %02d-%02d-%02d %02d:%02d:%02d\n",ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);}
const double pi = acos(-1.0);
const LL kInf(LONG_LONG_MAX);
const LL kMod(998244353);
const LL kMax(1e5 + 10);
LL group(1);
int dis(string s)
{
sort(s.begin(),s.end());
return unique(s.begin(),s.end()) - s.begin();
}
string find(const string &s,char c)
{
string t;
bool flag = false;
for(auto it:s)
{
if(it != c&&flag)
t+=it;
else if(it == c)
flag = true;
}
return t;
}
inline void Solve()
{
/*write your code down here*/
string str;
string res;
cin>>str;
int cnt = dis(str);
unordered_set<char>used(str.begin(),str.end());
while(cnt)
{
char maxx = CHAR_MIN;
for(char c:used)
{
if(dis(find(str,c)) == cnt-1)
{
maxx = max(maxx,c);
}
}
res += maxx;
str = find(str,maxx);
used.erase(maxx);
cnt--;
}
cout<<res<<'\n';
}
int main()
{
Read(group);
//gettime();
while (group--)
{
Solve();
}
return 0;
}