Codeforces Round #689 (Div. 2, based on Zed Code Competition): 1461A String Generation

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值