如何从字符列表中有效地提取固定长度的字符串排列?

问题描述 投票:0回答:1

我正在尝试创建一个从字符列表填充数组的函数,这样该数组将由n长度唯一字符串组成。应包括每种可能的排列。下面的工作示例使用n = 2但是我希望能够在运行时改变n

static char ULC[62] =   {'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','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','0','1','2','3','4','5','6','7','8','9'};

static char pw[4096][2];    

for (int i = 0; i < 62; i++)
    {
    for (int j = 0; j < 62; j++)
        {
        pw[i * 62 + j][0] = ULC[i];
        pw[i * 62 + j][1] = ULC[j];
        }   
    }

显然增加n将需要更大的数组,但我只是在上面的例子中使用静态数组来简化代码和解释。理想情况下,它可以在visual studio C中运行(不是C ++,绝对不是python,java等)。

c string permutation
1个回答
1
投票

如果你像这样重写它,那么使它完全动态化应该相当简单:

const int num_chars = 62;
const int n = 3;
static char pwn[num_chars * num_chars * num_chars][n];

for (int i = 0; i < num_chars * num_chars * num_chars; ++i) {
    int val = i;
    for (int in = 0; in < n; ++in) {
        pwn[i][in] = ULC[val % num_chars];
        val /= num_chars;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.