Codeforces 1506G Maximize the Remaining String 思维

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;
}

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值