D. Min Cost String
如果有k
个字母能用, 那么可以去构造一个长度为k * k
的序列使其价值为0
(即不存在s[i] == s[j] && s[i + 1] == s[j + 1]
).
如何构造? 比如当k = 3
时:
\(s:aabacbbcc\) 这个序列的价值为0
.
再如k = 4
:
\(s:aabacadbbcbdccdd\)
我们用以上方式构造, 如果不过n
个, 那么再从头开始往后复制即可.
code
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
char a[26][55];
char str[300010];
int main() {
//freopen("in.txt", "r", stdin);
for (int i = 0; i < 26; ++i) {
a[i][0] = 'a' + i;
int t = i + 1;
for (int j = 1; j < (26 - i - 1) * 2 + 1; ++j)
if (j % 2 == 0) a[i][j] = 'a' + t++;
else a[i][j] = 'a' + i;
}
//for (int i = 0; i < 26; ++i) {
//for (int j = 0; j < 55; ++j)
//cout << a[i][j];
//cout << '\n';
//}
int n, k, cnt = 0;
cin >> n >> k;
for (int i = 0; i < k; ++i)
for (int j = 0; j < (k - 1 - i) * 2 + 1; ++j)
str[cnt++] = a[i][j];
int j = 0;
while (cnt < n) str[cnt++] = str[j++];
for (int i = 0; i < n; ++i) cout << str[i];
cout << '\n';
return 0;
}