CF805B 3-palindrome 题解

Content

给定一个整数 \(n\),请构造出长度为 \(n\) 的仅含 abc 三个字母的字符串,使得其中没有长度为 \(3\) 的回文子串,并且 c 出现的次数尽可能少。

数据范围:\(1\leqslant n\leqslant 2\times 10^5\)

Solution

分析不难发现,我们可以构造出满足题意的字符串,使得 c 不会出现在这个字符串中。下面给出我的一种构造方案。

直接循环输出 abba,比如说 \(n=8\) 时输出 abbaabba\(n=9\) 时输出 abbaabbaa,这样可以保证不会出现长度为 \(3\) 的回文子串并且不会出现 c

下面给出极为简洁的代码。

Code

const string ans = "abba";

int main() {
	int n = Rint;
	F(int, i, 0, n - 1) putchar(ans[i % 4]);
    return 0;
}
posted @ 2021-12-15 22:14  Eason_AC  阅读(33)  评论(0)    收藏  举报