简体   繁体   English

字符串的构造

[英]Construction of a string

You are given three positive integers n, a and b.给你三个正整数 n、a 和 b。 You have to construct a string s of length n consisting of lowercase Latin letters such that each substring of length a has exactly b distinct letters.您必须构造一个长度为 n 的字符串 s,由小写拉丁字母组成,这样每个长度为 a 的 substring 都有 b 个不同的字母。

Please provide hints on how to solve.请提供有关如何解决的提示。 Please dont solve the question.请不要解决问题。 Should I use arrays?我应该使用 arrays 吗?

No. This is an easy problem, not necessary to use any external memory like-array or others.不,这是一个简单的问题,不需要使用任何外部 memory 类似阵列或其他。

You need just to iterate the loop and generate the characters so that you can construct a string following given conditions.您只需要迭代循环并生成字符,以便您可以按照给定条件构造字符串。 As you are generating characters, you can use ASCII values per iteration with loop to print the characters sequentially (not randomly, otherwise you may confuse).当您生成字符时,您可以使用每次迭代的 ASCII 值和循环来顺序打印字符(不是随机的,否则您可能会混淆)。


Again no need to think about the occurrence of exact b numbers of characters in each substrings as you are only generating the b number of characters each time.再次无需考虑每个子字符串中确切 b 个字符的出现,因为您每次只生成 b 个字符。

Hopeful to your understanding希望得到您的理解


example : (in C++)示例:(在 C++ 中)

for(int i = 0;  i < n; ++i)
 cout<<char('a'+i%b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM