Codeforces 1493C K-beautiful Strings

1493C
题意:修改一个字符串,最小化字符串且大于等于原字符串,同时每一个出现的字符数量为K的倍数
思路:①:最小化且大于等于原字符串,因此考虑从后往前枚举,能不修改尽量不修改;
②:如上限制,走到一个字符的时候向上枚举,并看后面的字符能不能修改成满足K的倍数。如果可以就向后填字符,否则继续向前枚举位置;
③:字符数量用前缀和实现判断。

#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 int kMax = 1e5 + 10;
LL group(1);
char str[kMax];
int pre[kMax][27];
int n,k;
int tonum(char c)
{
    return c-'a'+1;
}
LL getnum(int num)
{
    return (k-num%k)%k;
}
inline void Solve()
{
    /*write your code down here*/
    cin>>n>>k;
    scanf("%s",str+1);
    if(n%k)
    {
        puts("-1");
        return;
    }
    if(k == 1)
    {
        puts(str+1);
        return;
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= 26;j++)
        {
            pre[i][j] = 0;
        }
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= 26;j++)
        {
            pre[i][j] = pre[i-1][j];
        }
        pre[i][tonum(str[i])]++;
    }
    LL sum = 0;
    for(int i = 1;i <= 26;i++)
    {
        sum += getnum(pre[n][i]);
    }
    if(sum == 0){
        cout<<str+1<<'\n';
        return;
    }
    bool flag = false;
    for(int i = n;!flag && i >= 1;i--)
    {
        for(char c = str[i]+1;c <= 'z';c++)
        {
            LL tmp = tonum(str[i]);
            LL cur = tonum(c);
            pre[i][tmp]--,pre[i][cur]++;
            sum = 0;
            for(int j = 1;j <= 26;j++)
                sum += getnum(pre[i][j]);
            if(sum <= n-i+1)
            {
                for(int fuck = 1;fuck <= i-1;fuck++)
                cout<<str[fuck];
                cout<<c;
                LL rest = n-i-sum;
                for(int fuck = 1;fuck <= rest;fuck++)cout<<'a';
                for(char cc = 'a';cc <= 'z';cc++)
                {
                    sum = getnum(pre[i][tonum(cc)]);
                    while(sum--)cout<<cc;
                }
                cout<<'\n';
                return;
            }
            pre[i][tmp]++;
            pre[i][cur]--;
        }
    }
    cout<<-1<<'\n';
}

int main()
{
    Read(group);
    //gettime();
    while (group--)
    {
        Solve();
    }
    return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值