Codeforces Round #689 (Div. 2, based on Zed Code Competition): 1461A String Generation
题目:
One fall day Joe got bored because he couldn’t find himself something interesting to do. Marty suggested Joe to generate a string of length 𝑛 to entertain him somehow. It didn’t seem particularly difficult, but Joe’s generated string had to follow these rules:
the string may only contain characters 'a', 'b', or 'c';
the maximum length of a substring of this string that is a palindrome does not exceed 𝑘.
A string 𝑎 is a substring of a string 𝑏 if 𝑎 can be obtained from 𝑏by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. For example, strings “a”, “bc”, “abc” are substrings of a string “abc”, while strings “ac”, “ba”, “cba” are not.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, strings “abccba”, “abbba”, “aba”, “abacaba”, “a”, and “bacab” are palindromes, while strings “abcbba”, “abb”, and “ab” are not.
Now Joe wants to find any correct string. Help him! It can be proven that the answer always exists under the given constraints.
输入:
Each test contains one or more test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤10).
The only line of each test case contains two integers 𝑛 and 𝑘 (1≤𝑘≤𝑛≤1000) — the required string length and the maximum length of a palindrome substring, respectively.
输出:
For each test case, print any string that satisfies the conditions from the problem statement. If there are multiple correct answers, you can print any one of them. It can be proven that the answer always exists under the given constraints.
例子:
Input:
2
3 2
4 1
Output:
aab
acba
解释:
思路还是很简单的,就是对于其中子字符串且为回文的最大长度的字符串,不妨令其为最简单的重复元素字符串,例如aaa…,然后保证其它长度再长的子字符串均不是回文序列,就cbacba这样不构成回文的字符串周期连接而成即可。
具体实现方法是通过模除实现周期输出。
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
int k;
cin>>n>>k;
for(int i = 0;i < k;i++)
{
cout<<"a";
}
for(int i = 0;i < n - k;i++)
{
char a = 'c';
a = a - i%3;
cout<<a;
}
cout<<endl;
}
return 0;
}