场掉了かふ

· · 题解

省流:人均题。

题面

给定一个长度为 n 的字符串 s,保证对于所有 i \in [1,n] 都有 s_i \in \{\texttt{a},\texttt{*}\}。

现在可以将字符串中的每个字符 \texttt{*} 替换为 [0,m] 个字符 \texttt{b},求全部替换后能得到的字典序第 k 小的字符串。

思路

字典序优先考虑相同位置的字符大小,所以两个字符 \texttt{a} 之间隔得越近字典序越小。

考虑以下情况:

\large {\color{RED}\texttt{a}}\overbrace{\texttt{bbb}\dots}^x{\color{RED}\texttt{a}}\overbrace{\texttt{bbb}\dots}^y{\color{RED}\texttt{a}}

对于 x=x',y 为任意数的任意字符串,它们的字典序显然都没有 x=x'-1 时的字符串小。

所以越前的字符优先级越高;考场思路是以每两个字符 \texttt{a} 之间的间隔为单位,以 \texttt{*} 的个数为数值,将整个字符串看作一个不定进制数。

对于一个长度为 t 的字符 \texttt{*} 连续段,可以将其转换为 [0,t\times m] 个字符 \texttt{b},所以这一位就是 t\times m + 1 进制。

按照进制转换的基础思想,用一个循环依次求出 k 在各进制下应该为多少即能得到这个间隔应该转为多少个 \texttt{b}。

关键代码形如:

for (int i = scnt; i; i --)
    ot[i] = k % cnt[i], k /= cnt[i];
/*
scnt : 间隔数
ot[i] : 第 i 个间隔里应该填多少个字符 b
cnt[i] : 第 i 个间隔的进制是什么
*/

Tips:由于有 0 这个值,所以字典序(大小)第 k 大的数是值为 k-1 的数。

代码

CF Record.

#include <bits/stdc++.h>
#define break ({break;})
#define continue ({continue;})
#define return(x) ({return x;})

using namespace std;
typedef long long i64;
typedef double f16;
typedef pair <int, int> pii;

const int N = 2e3 + 10;
int n, m, cnt[N], ot[N], scnt;
string s;
i64 k;

void Solve ()
{
    cin >> n >> m >> k >> s;
    scnt = 0;
    s = " " + s; int last = 0;
    for (int i = 1; i <= n; i ++)
        if (s[i] == 'a')
        {
            if (i == 1) last = 1, continue;
            cnt[++ scnt] = i - 1 - last;
            last = i;
        }
    cnt[++ scnt] = n - last;
    for (int i = 1; i <= scnt; i ++)
        cnt[i] = cnt[i] * m + 1;
    k --;
    for (int i = scnt; i; i --)
        ot[i] = k % cnt[i], k /= cnt[i];
    for (int i = 1; i <= scnt; i ++)
    {
        if (!(i == 1 && s[1] == '*')) cout << 'a';
        while (ot[i] --) cout << 'b';
    }
    cout << '\n';
}

int main ()
{
    int T; cin >> T;
    while (T --) Solve ();
    return 0;
}