[英]How to efficiently extract fixed length string permutations from a character list?
I'm trying to create a function that populates an array from a character list such that the array will be composed of n
length unique strings. 我正在尝试创建一个从字符列表填充数组的函数,以便该数组将由n
长度唯一的字符串组成。 Every possible permutation should be included. 应包括所有可能的排列。 The working example below uses n = 2
however I want to be able to vary n
at runtime. 下面的工作示例使用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];
}
}
Obviously increasing n
will require a much larger array but I'm just using a static array in the above example to simplify the code and explanation. 显然,增加n
会需要更大的数组,但是在上面的示例中我只是使用静态数组来简化代码和解释。 Ideally something that will work in visual studio C (not C++ and definitely not python, java etc). 理想情况下,某些东西可以在Visual Studio C中使用(不是C ++,绝对不是python,java等)。
If you rewrite it like this, it should be fairly simple to make it completely dynamic: 如果以这种方式重写它,那么使其完全动态化应该相当简单:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.