计算给定字符集的所有组合,以进行powershell匹配?

在练习multithreading时,我曾希望简单地构建一个可以计算字符集的所有可能组合(即powershell破解/匹配)和在线程之间分配工作的应用程序,以真正地测量并亲自了解线程如何影响算法在不同系统上的时间。

到目前为止,计算这个算法对我来说是一个很大的挑战。 在最近的一个线程( 什么是一个有效的方法来添加multithreading到这个简单的算法? )我似乎得到了我需要做的事情(轻松传递每个字符范围的特定部分来分配工作)虽然算法根本不起作用,我不明白复杂性足以在我的应用程序中修复它。

以简单,迭代的方式,我如何计算给定字符集的每个组合,具有特定长度(即长度为5?)

在示例中:

unsigned char range[] = "abcdefghijklmnopqrstuvwxyz0123456789"; brute_force(range, len); //character set, length of string to compute all combinations of //... 

我非常感谢能够减轻一些关于找到这样做的正确概念的压力。

一种方法:

  void brute_force(String range,int len){
         for(int i = 0; i 

其中brute_force(String, String, int)将生成组合。

Straightfoward迭代强制执行5个元素:

 for c1 in set { for c2 in set { for c3 in set { for c4 in set { for c5 in set { test(c1,c2,c3,c4,c5); }}}}} 

要在线程之间划分工作,只需为每个线程提供一个sepatare“beggining part”。 因此,第一个线程处理所有以’a’开头的病房,第二个线程处理’b’等等。 (如果你有超过26个线程,那么第一个得到’aa’秒’ab’等等……


如果你想要一个可以随着长度更好地扩展的解决方案,那么最好是逐步解决问题(如果你想,可以使用显式堆栈将其转换为版本):

 unsigned char charset = /**/ unsigned int setsize = sizeof charset; bool test(combination); function bruteforce(output, n){ /* Goes through all character combinations of length n, writing them in output and calling test on them afterwards */ if(n == 0){ test(output); }else{ for(int i=0; i