C# 使用通配符生成单词的所有排列
我需要恢复我的密码,我知道一些字母,但忘了我在其余部分使用了什么字符。我需要一种方法来生成所有可能的密码排列,给定一些已知字符和一些未知字符。例如,我想在文本框中输入一个短语,如“mic??s??t”,程序将生成该短语的所有可能排列。在这种情况下,我知道只有几个字符可能被用来代替那些问号,但我希望程序能够选择将所有字符a-Z、a-Z、0-9、/symbols/等,或一组特定的字符(如E-G、t-y、1-4等)作为字符串通过第二个文本框进行排列 使用所有字符,它可能会生成如下列表 米卡萨特 米卡萨布特 米卡萨克 米卡萨达特 仅使用有限的字符集(如E-G)看起来像 米切塞特 米切塞夫特 米切塞格特 米塞斯菲 米塞斯夫特C# 使用通配符生成单词的所有排列,c#,wildcard,permutation,combinations,C#,Wildcard,Permutation,Combinations,我需要恢复我的密码,我知道一些字母,但忘了我在其余部分使用了什么字符。我需要一种方法来生成所有可能的密码排列,给定一些已知字符和一些未知字符。例如,我想在文本框中输入一个短语,如“mic??s??t”,程序将生成该短语的所有可能排列。在这种情况下,我知道只有几个字符可能被用来代替那些问号,但我希望程序能够选择将所有字符a-Z、a-Z、0-9、/symbols/等,或一组特定的字符(如E-G、t-y、1-4等)作为字符串通过第二个文本框进行排列 使用所有字符,它可能会生成如下列表 米卡萨特 米
如果这样做的唯一方法是为一个N长度的字生成每个排列周期(通配符与否),然后使用正则表达式模式检查每个排列周期以过滤掉无用的排列周期,我可以接受这一点(尽管它会生成256^N个可能的组合)。否则,我宁愿使用递归生成所有可能的数组(我需要帮助)。最后,我想生成这些排列的txt文件列表。我真的需要这里的递归帮助。我用C#。它实际上是组合而不是排列 无论如何,为什么要使用递归 使用递归是因为我们可以想到一种以递归方式工作的解决方案,因此我们编程以匹配。通常,编译器无法将其优化为更快、更小的迭代版本,但这通常很好。但是,如果您自己不能想到递归方法,那么为什么要寻求它的帮助,而不是迭代方法呢
private IEnumerable<string> Combinations()
{
//Let's just use a-c for a test run.
string setOfChars = "abc";
//Let's return for four characters to guess for. We can change this.
char[] buffer = new char[4];
//We'll change these indices and then use them to pick chars from
//setOfChars to the corresponding place in buffer
int[] setOfIndices = new int[buffer.Length];
//set up initial position:
for(int i = 0; i != buffer.Length; ++i)
buffer[i] = setOfChars[0];
//return our inital position.
yield return new string(buffer);
//Now for our actual combinations.
for(int i = 0; i != buffer.Length; ++i)
{
if(++setOfIndices[i] == setOfChars.Length)
//if we've pushed an index out of range, then set it back to zero.
setOfIndices[i] = 0;
else
{
//otherwise move our position for changing things back to zero
i = -1;
//and generate our new combination.
for(int j = 0; j != buffer.Length; ++j)
buffer[j] = setOfChars[setOfIndices[j]];
yield return new string(buffer);
}
}
}
私有IEnumerable组合()
{
//让我们使用a-c进行测试运行。
字符串setOfChars=“abc”;
//让我们返回四个字符来猜测。我们可以改变这个。
char[]buffer=新字符[4];
//我们将更改这些索引,然后使用它们从中拾取字符
//设置缓存中相应位置的缓存
int[]setOfIndices=newint[buffer.Length];
//设置初始位置:
for(int i=0;i!=buffer.Length;++i)
缓冲区[i]=setOfChars[0];
//返回初始位置。
返回新字符串(缓冲区);
//现在来看看我们的实际组合。
for(int i=0;i!=buffer.Length;++i)
{
if(++setOfChars[i]==setOfChars.Length)
//如果我们把一个索引推到了范围之外,那么就把它设置回零。
setOfIndices[i]=0;
其他的
{
//否则,将我们改变事物的位置移回零
i=-1;
//并生成我们的新组合。
对于(int j=0;j!=buffer.Length;++j)
缓冲区[j]=setOfChars[SetOfDices[j]];
返回新字符串(缓冲区);
}
}
}
这将给我们81个不同的字符串(3的四次方)。确实如此。在其上调用
Distinct()
,以确保bug没有创建重复项,仍然会给我们带来81。快乐时光。这里有一个简单的解决方案,它融合了递归和迭代方法。我认为可以实现完全递归的解决方案,但我发现这种方法更容易理解
//List of characters to substitute in place of '?'
List<char> validChars = new List<char>() { 'w', 'x', 'y', 'z' };
//List of combinations generated
List<string> combos = new List<string>();
void GenerateCombos(string mask, string combination)
{
if (mask.Length <= 0)
{
//No more chars left in the mask, add this combination to the solution list.
combos.Add(combination);
return;
}
if (mask[0] != '?')
{
//This is not a wildcard, append the first character of the mask
//to the combination string and call the function again with
//the remaining x characters of the mask.
GenerateCombos(mask.Substring(1), combination + mask[0]);
}
else
{
//This is a wildcard, so for each of the valid substitution chars,
//append the char to the combination string and call again
//with the remaining x chars of the mask.
validChars.ForEach(c => GenerateCombos(mask.Substring(1), combination + c));
}
}
这实际上是问号数的幂<代码>ABCD?在哪里?只能是A-Z(26),最大组合为26^1。
string mask = "mic??s??t";
string combination = String.Empty;
GenerateCombos(mask, combination);