字符串生成

发布于 2024-09-02 13:57:32 字数 259 浏览 2 评论 0原文

我想知道如何从某些给定字符创建各种字符串,例如:

给定字符:ab

我想生成以下字符串:

aa
ab
ba
bb

我的想法of 具有(仅适用于 2 个输入)两个 for 循环,一个在另一个内,然后将每个循环循环到输入数量(在本例中为 2),输出字符串将为 2*2 = 4 个字符串,并且随着数量的增加,输出字符串的数量将通过乘以 n*n(n 倍)来增加

I would like to know how can I create various string from some given characters eg:

given characters: a, b

I would like to generate the following strings:

aa
ab
ba
bb

What I have thought of is having (for 2 inputs only) two for-loops one inside another, and then loop each to the number of inputs which in this case is 2 and the output strings will be 2*2 = 4 strings and as the number increases the number of output strings will increase by multiplying n*n (n-times)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

年华零落成诗 2024-09-09 13:57:32

这是 Kleene 闭包(Kleene star) 的变体(如果对您有帮助)寻找解决方案。

This is a variation of the Kleene closure (Kleene star) if that helps you in finding a solution.

兮子 2024-09-09 13:57:32

你的方法(据我所知)作为第一次尝试听起来不错,尽管布丁的证明是吃它,所以编写代码并测试它:-)

请注意,它不会很好地扩展,所以问题是您期望生成多少个字符和多长的字符串。如果答案是“不多”,并且性能/内存消耗不是问题,那么坚持使用最简单的解决方案就可以了。否则,您需要更复杂的算法。

前段时间我曾经做过一个隐约类似的任务,其中可能的排列数量如此之大,以至于根本没有足够的内存来同时包含每个排列。因此,我们尝试用数字对排列进行建模:请注意,任何 nm 个字符的长排列都可以使用 m 基数 < em>n 位数字。因此,通过迭代从 0 到 mn 的所有整数值,调用一个相当简单的转换方法即可一一获取每个可能的字符串。当然,对于索引值,您可能需要使用更大的整数类型,例如 long long 来表示更大的 mn 值。

Your approach (as much as I understood of it) sounds good as a first attempt, although the proof of the pudding is eating it, so write the code and test it :-)

Note that it won't scale very well, so the question is how many chars and how long strings you expect to be generated. If the answer is "not many", and performance / memory consumption is not an issue, it is fine to stick with the simplest solution which works. Otherwise, you need a more sophisticated algorithm.

I've had a vaguely similar task some time ago, where the number of possible permutations was so large that there was simply not enough memory to contain each at the same time. So we tried to model the permutations with numbers: note that any n long permutation of m characters can be defined with an m base number of n digits. So by iterating through all integer values from 0 until mn, calling a fairly simple conversion method gets you each possible string one by one. For the index value of course you might need to use a bigger integer type like long long for bigger m and n values.

虐人心 2024-09-09 13:57:32

我认为这是一个排列问题..

http://www.bearcave.com/random_hacks/permute .html

I think this is a permutation question..

http://www.bearcave.com/random_hacks/permute.html

逆光飞翔i 2024-09-09 13:57:32

使用递归(只是一个想法演示):

void generate(std::string& s, size_t beg) {
  for (char c = 'a'; c <= 'b'; ++c) {
    s[beg] = c;
    if (beg < s.length() - 1) {
      generate (s, beg + 1);
    }
    else {
      std::cout << s << std::endl;
    }
  }
}

int main() {
  std::string s = "####";
  generate(s, 0);
  return 0;
}

With recursion (just an idea demonstration):

void generate(std::string& s, size_t beg) {
  for (char c = 'a'; c <= 'b'; ++c) {
    s[beg] = c;
    if (beg < s.length() - 1) {
      generate (s, beg + 1);
    }
    else {
      std::cout << s << std::endl;
    }
  }
}

int main() {
  std::string s = "####";
  generate(s, 0);
  return 0;
}
妳是的陽光 2024-09-09 13:57:32

您可以使用 std::next_permutation。即使您重复字母(即字母=“ababcd”),这也将起作用。

#include <algorithm>
#include <iostream>
#include <string> 


int main(int argc, char** argv) {
    std::string letters = "abcd";

    std::sort(letters.begin(), letters.end());
    do {
        std::cout << letters << "\n";
    }
    while (std::next_permutation(letters.begin(), letters.end()));
}

You can use std::next_permutation. This will work even if you repeat letters (i.e. letters = "ababcd").

#include <algorithm>
#include <iostream>
#include <string> 


int main(int argc, char** argv) {
    std::string letters = "abcd";

    std::sort(letters.begin(), letters.end());
    do {
        std::cout << letters << "\n";
    }
    while (std::next_permutation(letters.begin(), letters.end()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文