Codeforces#689(Div.2)A. String Generation

Codeforces#689(Div.2)A. String Generation

题目描述:
A. String Generation
要求:
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
正文描述: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 n 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 k.

A string a is a substring of a string b if a can be obtained from b 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.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤10).

The only line of each test case contains two integers n and k (1≤k≤n≤1000) — the required string length and the maximum length of a palindrome substring, respectively.

Output
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.

Example
inputCopy
2
3 2
4 1
outputCopy
aab
acba
解析:
要求得出的符合要求的回文子字符串不超过一定范围
由题可知:只包含‘a’,‘b’,'c’三个字符,
该题可以分为两种情况:
如果k==1,则最大的回文子字符串长度为1,我们可以在n个字符内对‘a’,‘b’,'c’进行循环输出,直到长度满n为止,此时最大的回文子字符串的长度为1;
如果k>=2,则前k个字符可以重复输出‘a’,‘b’,'c’中的一个,剩下的n-k个对‘a’,‘b’,'c’进行循环输出,注意:循环输出的第一个字符与前k个输出的字符开头应该不同。如“aaaaabcabc",前k个输出’a’时,后面的循环应从‘b’或’c’开始。

#include<cstdio>
#include<bits/stdc++.h>
#include<string>
using namespace std;
int main()
{
	int T;
	scanf("%d",&T);
	for(int i=0;i<T;i++)
	{
		int N,M;
		scanf("%d %d",&N,&M);
		if(M==1)
		{
			int i=0;
			char a='a';//可以随便写一个,这里我写的为‘a’;
			while(i<N)
			{
				printf("%c",a);
				a++;
				if(a>'a'+2)
				{
					a='a';
				}
				i++;//因为只能输出‘a’,'b','c'中的一个
			}
		}
		else if(M>=2)
		{
			for(int i=0;i<M;i++)
			{
				printf("a");//可以随便定‘a’,'b','c'中的一个,与下两排的char中定义应不同;
			}
			int i=0;
			char a='b';
			while(i<N-M)
			{
				printf("%c",a);
				a++;
				if(a>'a'+2)
				{
					a='a';
				}
				i++;
			} 
		}
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值