c++ - 在 C++ 中生成单词表

标签 c++ word-list

我正在尝试找出如何根据给定的字符串在 C++ 中生成单词列表(类似于 crunch wordlist 的工作方式)

我是 C++ 的新手,只得到了一次列出一个字符的程序。

我已经在网上搜索了很长一段时间,除了找到 O(n^2) 之外没有太多运气,但对如何将它实现到程序中不太了解。

代码:

int main() {
    string characters = "abcde";

    int length = 5;
    string word = "";
    for(int i = word.length(); i <= length; i++) {
        for(int l = 0; l < characters.length(); l++) {
            word += characters[l];
            cout << word << "\n";
        }
    }
    return 0;
}

结果:

a
b
c
d
e

想要的结果:http://pastebin.com/tgyUtKfA

结果片段:

a
b
c
d
e
aa
ab
ac
ad
ae
ba
bb
bc
bd
be
ca
cb
cc
cd
ce
da
db
dc
dd
de
ea
eb
ec
ed
ee
aaa

(最终结果遵循该模式直至“eeeee”)

最佳答案

#include <string>
#include <vector>
#include <iostream>
using namespace std;

vector<string> make_permutations(int length) {
  if (length == 0) {
    return vector<string>{};
  }
  vector<string> results;
  vector<string> children = make_permutations(length - 1);
  for (char letter = 'a'; letter <= 'z'; letter++) { 
    results.emplace_back(1, letter);
    for(auto child : children) {
      results.push_back(std::string(1, letter) + child);
    }
  }
  return results;
}

int main()
{
 auto results = make_permutations(2);
    for(auto s : results) cout << s << endl;
}

直播:http://melpon.org/wandbox/permlink/gGVAxbVUFVZs4fUR

关于c++ - 在 C++ 中生成单词表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36718376/

相关文章:

c++ - C++标准库中有算术类型的概念吗?

c++ - std::vector<int>::clear,常数时间?

python - 随机词生成器 - Python

python - 以编程方式使用的免费单词列表?

Python bit.ly 链接词表生成器

c++ - 如何在 Qt 中连接不同对象的信号和槽?

c++ - 将 std::thread 与 std::mutex 一起使用

c++ - 使用 C++/WinRT 使用设备填充 ListBox,显示它们的名称?

java - 根据单词出现频率对列表进行排序

java - 支持大整数的单词列表生成器