把字符串翻转之后处理会更加方便
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define rep(i,a,b) for(ll i=(a);i<=(b);i++) #define dec(i,a,b) for(ll i=(a);i>=(b);i--) #define pll pair<ll,ll> using namespace std; ll INF = 0x7f7f7f7f7f7f7f7f; const int N = 2e5 + 5; ll mod = 998244353; ll n, k, x; string s; int main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(nullptr); ll T; cin >> T; while (T--) { cin >> n >> k >> x >> s; x--; reverse(s.begin(), s.end()); string ans = ""; ll i = 0; while (i < n) { if (s[i] == 'a') { ans += s[i]; } else { ll j = i; while (j + 1 < n && s[j + 1] == s[i]) { j++; } ll cur = (j - i + 1) * k + 1; rep(k, 1, x % cur) { ans += 'b'; } x /= cur; i = j; } i++; } reverse(ans.begin(), ans.end()); cout << ans << '\n'; } return 0; }