很毒瘤。
考虑递归构造。
首先k=1的时候,答案为a。
增加k时,将现有字符串中的字符全部顺移一位(aabaa变成bbcbb)
然后新字符串先放两个a,然后遍历旧字符串,每次遇到一个b就放ba,直到放完,然后再放一个a。
最后把不到n的部分拿a补上。
#include <assert.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using int_t = long long int;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
const int_t LARGE = 1e5;
void process() {
int_t n, k;
cin >> n >> k;
std::vector<char> buf;
buf.push_back('a');
for (int_t i = 1; i <= k - 1; i++) {
std::vector<char> next{'a', 'a'};
for (char chr : buf) {
next.push_back(chr + 1);
if (chr == 'a') {
next.push_back('a');
}
}
next.push_back('a');
buf = next;
}
#ifdef DEBUG
for (char chr : buf) cout << chr << " ";
cout << endl;
#endif
if (n < buf.size()) {
printf("CiYe\n");
return;
}
for (int_t i = 1; i <= n - buf.size(); i++) {
printf("a");
}
for (char chr : buf) printf("%c", chr);
cout << endl;
}
int main() {
int_t T;
cin >> T;
while (T--) {
process();
}
return 0;
}
发表回复