【问题标题】:C# word permutations with special characters带有特殊字符的 C# 单词排列
【发布时间】:2019-04-15 09:33:38
【问题描述】:

我正在尝试创建单词“密码”的排列,我已经设法使用下面的代码创建了单词的所有排列。目前这是大写和小写。我需要包括具有特殊字符的排列,例如我将有“p@ssword”的位置。我用“@”和“o”“0”替换“a”。

知道如何扩展我的实现吗?

 public static void WritePermutations(string pwd)
    {
        pwd = pwd.ToLower();
        var myDict = new Dictionary<int, string>();
        int Count = 0;

        var results =
            from e in Enumerable.Range(0, 1 << pwd.Length)
            let p =
            from b in Enumerable.Range(0, pwd.Length)
            select (e & (1 << b)) == 0 ? (char?)null : pwd[b]
            select string.Join(string.Empty, p);

        foreach (string s in results)
        {
            string newValue = pwd;
            s.ToLower();
            foreach (char c in s)
            {
                var Old = c.ToString().ToLower();
                var New = c.ToString().ToUpper();
                newValue = ReplaceFirstOccurrence(newValue, Old, New);
                Count++;
            }

            myDict.Add(Count, newValue);
        }

            foreach (var cred in myDict)
            {

                Console.WriteLine(cred.Value);
            }
    }

  public static string ReplaceFirstOccurrence(string Source, string Find, string Replace)
    {
        int Place = Source.IndexOf(Find);
        string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
        return result;
    }

【问题讨论】:

  • 对于每个使用的字母(PASWORD),制作一个替代数组..遍历所有组合
  • @BugFinder 我不确定我是否理解,所以我创建了一个带有特殊字符(替代)的数组?
  • 选择是无穷无尽的,但是是的,为每个要替换的字符创建一个数组,并用可以放入其中的替代项填充它,所以,o 可能会得到 o, ó, 0, O, ()。 .
  • 好的,所以在我的情况下,它只有两个字符,可选 a = @ 和 o = 0(零),我将如何将它包含到我的 newValue 中,因为我已经改变了特点。在我已经存在的迭代中如何更改案例?
  • 对每个可用选项运行相同的代码..

标签: c# .net permutation


【解决方案1】:

如何从这些声明和函数开始:

    List<HashSet<char>> _charCombinations = new List<HashSet<char>> {
        new HashSet<char> {'a','@'},
        new HashSet<char> {'o', '0'},
    };

    HashSet<char> GetAlternatives(char c)
    {
        var result = new HashSet<char>();
        foreach (var hashSet in _charCombinations)
        {
            if (hashSet.Contains(c))
            {
                foreach (char c2 in hashSet)
                    result.Add(c2);
            }
        }

        if (char.IsLetter(c))
        {
            result.Add((String.Empty + c).ToUpper()[0]);
            result.Add((String.Empty + c).ToLower()[0]);
        }
        else if (false) // any other char.Is-based logic
        {

        }
        result.Add(c);
        return result;
    }

    IEnumerable<string> GetTransformations(string s, int start)
    {
        char c = s[start - 1];
        foreach (var c2 in GetAlternatives(c))
        {
            if (start == s.Length)
                yield return String.Empty + c2;
            else
            {
                var e = GetTransformations(s, start + 1).GetEnumerator();
                while (e.MoveNext())
                    yield return  c2 + e.Current;

            }
        }
    }

然后你可以像这样使用它们:

        var e = GetTransformations("password", 1).GetEnumerator();
        var result = new List<string>();
        while (e.MoveNext())
            result.Add(e.Current);
        result.Sort((a,b) => string.CompareOrdinal(a, b));

这会产生 576 个字符串(太长,无法在此处列出),这正是您对 8 个字母的单词所期望的,其中 6 个字符有 2 种可能性,另外 2 个有 3 种可能性,即 2x2x2x2x2x2x3x3

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2020-02-29
    • 2019-12-13
    • 2020-06-06
    • 1970-01-01
    • 2017-12-17
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多