You are given an integer kk and a string ss that consists only of characters 'a' (a lowercase Latin letter) and '*' (an asterisk).
Each asterisk should be replaced with several (from 00 to kk inclusive) lowercase Latin letters 'b'. Different asterisk can be replaced with different counts of letter 'b'.
The result of the replacement is called a BA-string.
Two strings aa and bb are different if they either have different lengths or there exists such a position ii that ai≠biai≠bi.
A string aa is lexicographically smaller than a string bb if and only if one of the following holds:
- aa is a prefix of bb, but a≠ba≠b;
- in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.
Now consider all different BA-strings and find the xx-th lexicographically smallest of them.
Input
The first line contains a single integer tt (1≤t≤20001≤t≤2000) — the number of testcases.
The first line of each testcase contains three integers nn, kk and xx (1≤n≤20001≤n≤2000; 0≤k≤20000≤k≤2000; 1≤x≤10181≤x≤1018). nn is the length of string ss.
The second line of each testcase is a string ss. It consists of nn characters, each of them is either 'a' (a lowercase Latin letter) or '*' (an asterisk).
The sum of nn over all testcases doesn't exceed 20002000. For each testcase xx doesn't exceed the total number of different BA-strings. String ss contains at least one character 'a'.
Output
For each testcase, print a single string, consisting only of characters 'b' and 'a' (lowercase Latin letters) — the xx-th lexicographically smallest BA-string.
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:
- a
- ab
- abb
- abbb
- abbbb
In the second testcase of the example, BA-strings ordered lexicographically are:
- aa
- aba
- abba
Note that string "aba" is only counted once, even though there are two ways to replace asterisks with characters 'b' to get it.
思路:从最后开始变*是最小的,而且最小的是只有a,这个题需要倒着做,不难看出总共的变换次数是最后一堆的个数乘以前面的,我们正着做会发现让它停下来(当总和大于x时)又是特别麻烦,本来这题就是以后面的为主(因为越考后越小,符合题意)我们应当用除余得到来自相加的多余的数(这里的相加是不满,不是最后一堆的整数倍)除法能够将乘法抵消。这里还有一点需要注意是:x是从1开始的,当x等于1时,总不能在往字符串上加个b吧,并且我们计算总个数时是从0开始的,而x(个数)是从1开始的,所以要将我们计算的个数与题要求的个数相对齐,让x-1,刚好x=1时输出0个b
乘法与加法得某个数 变 由某个数除余和除法
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
void solve()
{
ll n,k,x;
cin>>n>>k>>x;
string s;
cin>>s;
x--;
reverse(s.begin(),s.end());
string res="";
ll i=0;
while(i<n)
{
if(s[i]=='a')
{
res+=s[i];
}
else
{
ll j=i;
while(j+1<n&&s[j+1]==s[i])
{
j++;
}
ll cur=(j-i+1)*k+1;
for(ll p=1;p<=(x%cur);p++)
{
res+='b';
}
i=j;
x/=cur;//每块之间是乘法,要想算出之后还有多少,就要用除法
}
i++;
}
reverse(res.begin(),res.end());
cout<<res<<endl;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}