C. BA-String(字符串转换为K进制位)

Problem - C - Codeforces

 给你一个整数k和一个字符串s,它只包含字符'a'(一个小写拉丁字母)和'*(一个星号)。每个星号应替换为几个小写拉丁字母“b”(从O到k包括在内)。不同的星号可以替换为不同数量的字母“b”。替换的结果称为BA-string。两个字符串a和b是不同的,如果它们的长度不同,或者存在这样一个位置i, a;bi字符串A在字典上小于字符串b当且仅当下列情况之一成立:A是b的前缀,但A是b;在a和6不同的第一个位置,字符串a的字母出现在字母表中比对应的字母b更早。现在考虑所有不同的ba字符串,找出它们中按字典顺序最小的第-个。输入第一行包含一个整数t (1 <t < 2000) -测试用例的数量。每个测试用例的第一行包含三个整数n, k和a (1 <n < 2000;0 < k < 2000;1 <a < 1018)。N是字符串s的长度。每个测试用例的第二行是一个字符串s。它由n个字符组成,每个字符都是'a'(小写拉丁字母)或'*'(星号)。所有测试用例的n和不超过2000。对于每个测试用例,z不超过不同ba -string的总数。字符串至少包含一个字符“a”。输出对于每个测试用例,打印一个字符串,只包含字符'b'和'a'(小写拉丁字母)-第a个lexicographicallyl最小的ba -字符串。

Example

input

Copy

3
2 4 3
a*
4 1 3
a**a
6 3 20
**a***

output

Copy

abb
abba
babbbbbbbbb

Note

In the first testcase of the example, BA-strings ordered lexicographically are:

  1. a
  2. ab
  3. abb
  4. abbb
  5. abbbb

In the second testcase of the example, BA-strings ordered lexicographically are:

  1. aa
  2. aba
  3. abba

Note that string "aba" is only counted once, even though there are two ways to replace asterisks with characters 'b' to get it.

题解:

由于填充的都是b,原字符串只有a与*,那么肯定是从后往前填b是最优的

但是由于x很大,肯定不能暴力写,

首先原字符串肯定是第一小的,所以--

举个例子

a*a* k = 2 

从小到大应该是

aa aab aabb

aba abab ababb

abba abbab abbabb

这不就是k进制位的形式吗,我们记录每两个a之间有多少b,每次遇到a进位

模拟这个过程即可

#include<iostream>
#include<string>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
char s[200050];
void solve() 
{
	ll n,k,x;
	scanf("%lld%lld%lld",&n,&k,&x);
	scanf("%s",s);
	ll c[2005]={0};
	ll b[2005]={0};
	ll t = 0;
	int cnt = 0;
	x--;
	s [n] = 'a';
	for(int i = 0;i <= n;i++)
	{
		if(s[i] == 'a')
		{
			c[++cnt] = k*t;
			t = 0;
		}
		else
		{
			t++;
		}
	}
	for(int i = cnt;i >= 1;i--)
	{
		if(c[i])
		{
			ll w = c[i] + 1;
			b[i] = x%w;
			x /= w;
		}
	}
	for(int i = 1;i <= cnt;i++)
	{
		for(int j = 1;j <= b[i];j++)
		printf("b");
		if(i!=cnt)
		printf("a");
	}
	printf("\n");
}
 
//1 2 4
signed main() 
{
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int t = 1;
//	cin >> t;
    scanf("%d",&t);
	while (t--) 
	{
		solve();
	}
	return 0;
}
//1 1 1 0 1
 
//1 1 1 0 1
//1 1 1 0 1
//1 1 1 0 1
//0 1 1 1 1
//0 1 1 1 1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值