Codeforces Round #634 B. Construct the String(贪心)
题目描述
You are given three positive integers n n n , a a a and b b b . You have to construct a string s s s of length n n n consisting of lowercase Latin letters such that each substring of length a a a has exactly b b b distinct letters. It is guaranteed that the answer exists.
You have to answer t t t independent test cases.
Recall that the substring s[l…r] s[l \dots r] s[l…r] is the string sl,sl+1,…,sr s_l, s_{l+1}, \dots, s_{r} sl,sl+1,…,sr and its length is r−l+1 r - l + 1 r−l+1 . In this problem you are only interested in substrings of length a a a .
输入格式
The first line of the input contains one integer t t t ( 1≤t≤2000 1 \le t \le 2000 1≤t≤2000 ) — the number of test cases. Then t t t test cases follow.
The only line of a test case contains three space-separated integers n n n , a a a and b b b ( 1≤a≤n≤2000,1≤b≤min(26,a) 1 \le a \le n \le 2000, 1 \le b \le \min(26, a) 1≤a≤n≤2000,1≤b≤min(26,a) ), where n n n is the length of the required string, a a a is the length of a substring and b b b is the required number of distinct letters in each substring of length a a a .
It is guaranteed that the sum of n n n over all test cases does not exceed 2000 2000 2000 ( ∑n≤2000 \sum n \le 2000 ∑n≤2000 ).
输出格式
For each test case, print the answer — such a string s s s of length n n n consisting of lowercase Latin letters that each substring of length a a a has exactly b b b distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists.
输入输出样例
4 7 5 3 6 1 1 6 6 1 5 2 2
tleelte qwerty vvvvvv abcde
说明/提示
In the first test case of the example, consider all the substrings of length 5 5 5 :
- "tleel": it contains 3 3 3 distinct (unique) letters,
- "leelt": it contains 3 3 3 distinct (unique) letters,
- "eelte": it contains 3 3 3 distinct (unique) letters.
直接构造循环节长度为b的字符串,比如n=7 b=3 就构造abcabca 这样能在尽可能短的距离内让不同字符出现的次数比较均匀。和a没啥关系。
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { int n,a,b; scanf("%d%d%d",&n,&a,&b); int i,j; for(i=0;i<n;i++) { printf("%c",i%b+'a'); } cout<<endl; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!