Ç - 生成的x字符的话所有的可能性 [英] C - Generating all possibilities of X character words

查看:126
本文介绍了Ç - 生成的x字符的话所有的可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我的意思是置换,没有组合。谢谢你。

I meant Permutations, not combinations. Thanks.

我意识到这是一个相当开放式的问题,我不是在寻找每说code,但确实有些线索从哪里开始。我想是一个程序,它可以生成字符的每个组合为一个给定的长度,即用户输入4,和程序将生成的ASCII字符每个可能的组合为4的长度

I realize this is a rather open ended question, and I'm not looking for code per say, but really some hints where to start. What I want to make is a program, that can generate every combination of characters for a given length, i.e the user inputs 4, and the program will generate every possible combination of ASCII characters for a length of 4.

不能确定,我将开始,也许使用哈希表?当然,将需要的循环,但我不能确定如何设计他们生产的组合。截至目前为止,它一直是,循环的情况下,直到1000的事情已经发生了的例子。

Not really sure where I would start, perhaps the use of a hash table? Of course loops will be needed but I'm unsure how to design them to produce combinations. Up until now, its always been a case of, loop until 1000 things have happened for example.

任何意见得多AP preciated!

Any advice is much appreciated !

干杯,

T。

推荐答案

有关排列组合,你可以使用这样一个递归解决方案(这大概可以优化和改进):

For permutations, you can use a recursive solution like this (which can probably be optimised and improved):

unordered_set<string> permute_string(int n) {    
    static const char chars[] = {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    };

    unordered_set<string> s;

    if (n == 0) {
        s.insert("");

        return s;
    }

    unordered_set<string> perms = permute_string(n - 1);

    for (auto c = std::begin(chars); c < std::end(chars); ++c)
        for (auto i = perms.begin(); i != perms.end(); ++i)
            for (int pos = 0; pos < n; ++pos)
                s.insert(string(*i).insert(pos, 1, *c));

    return s;
}

请注意,这个函数的输出(无论你如何实现它)是的 26 N 的,这是456976时的 N 的(在该函数输入)为4。

Note that the output of this function (no matter how you implement it) is 26n, which is 456,976 when n (the input for this function) is 4.

这篇关于Ç - 生成的x字符的话所有的可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆