题目链接:hdu 5559 Frog and String
解题思路
- n < m:impossible
- n = m:用全A构造
- n > m:
- k = 1:impossible
- k = 2:用 AAAA….(m-8个A) + ABAABB ABAA…
- k > 3:用 AAAA….(m-3个A) + ABC ABC ABC …
特殊情况n = 8, m = 7, k = 2。
原则上是找到一种构造方式是不会在增加回文子串
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const char rep[][10] = {"A", "ABC", "ABAABB"};
const int siz[] = {1, 3, 6};
void draw(int id, int n) {
for (int i = 0; i < n; i++) {
int v = i % siz[id];
printf("%c", rep[id][v]);
}
}
int main () {
int cas, n, m, k;
scanf("%d", &cas);
for (int kcas = 1; kcas <= cas; kcas++) {
scanf("%d%d%d", &n, &m, &k);
printf("Case #%d:\n", kcas);
if (n < m) printf("Impossible");
else if (n == m) draw(0, n);
else {
if (k == 1) printf("Impossible");
else if (k == 2) {
if (n == 8 && m == 7) {
printf("AABABBAA");
} else if (m < 8) printf("Impossible");
else {
draw(0, m-8);
draw(2, n-m+8);
}
} else {
if (m < 3) printf("Impossible");
else {
draw(0, m-3);
draw(1, n-m+3);
}
}
}
printf("\n");
}
return 0;
}