如何改进anagram代码并使其更快?

时间:2018-05-18 05:46:52

标签: c# anagram

当超过10个字符时,程序运行缓慢,我想快速完成。我不明白我如何改进并寻求你的帮助。

并且仍然需要计算和编号那些没有出现在原始位置的组合,我想如果我添加这个逻辑,那么程序将工作更长时间,这也需要你的建议

P.S。抱歉我的英文)

private void btnGo_Click(object sender, EventArgs e)
    {
        this.Size = new Size(632, 430);
        button1.Visible = true;
        // Get the items.
        string[] items = txtItems.Text.Split(' ');
       // string[] items = txtItems.Text.ToString;
        // Generate the permutations.
        List<List<string>> results =
            GeneratePermutations<string>(items.ToList());
        //  results.Where(x => !HasCharacterAtSamePositionAsInOriginal(items, x));
        List<string> list = new List<string>();

        // Display the results.
        lstPermutations.Items.Clear();
        foreach (List<string> combination in results.Where(x => !HasCharacterAtSamePositionAsInOriginal(items, x)))
        {
            if (checkBox1.Checked == true)
            {
                lstPermutations.Items.Add(string.Join("", combination.ToArray()));
            }
            else
            {

                lstPermutations.Items.Add(string.Join(" ", combination.ToArray()));

            }

        }

        // Calculate the number of permutations.
        long  num_permutations = Factorial(items.Length);
        txtNumPermutations.Text = num_permutations.ToString();

        // Check the result.
     //  Debug.Assert(lstPermutations.Items.Count == num_permutations);

}

    private List<List<T>> GeneratePermutations<T>(List<T> items)
    {
        // Make an array to hold the
        // permutation we are building.
        T[] current_permutation = new T[items.Count];

        // Make an array to tell whether
        // an item is in the current selection.
        bool[] in_selection = new bool[items.Count];

        // Make a result list.
        List<List<T>> results = new List<List<T>>();

        // Build the combinations recursively.
        PermuteItems<T>(items, in_selection,
            current_permutation, results, 0);

        // Return the results.
        return results;
    }

    // Recursively permute the items that are
    // not yet in the current selection.
    private void PermuteItems<T>(List<T> items, bool[] in_selection,
        T[] current_permutation, List<List<T>> results, int next_position)
    {
        // See if all of the positions are filled.
        if (next_position == items.Count)
        {
            // All of the positioned are filled.
            // Save this permutation.
            results.Add(current_permutation.ToList());
        }
        else
        {
            // Try options for the next position.
            for (int i = 0; i < items.Count; i++)
            {
                if (!in_selection[i])
                {
                    // Add this item to the current permutation.
                    in_selection[i] = true;
                    current_permutation[next_position] = items[i];

                    // Recursively fill the remaining positions.
                    PermuteItems<T>(items, in_selection,
                        current_permutation, results, next_position + 1);

                    // Remove the item from the current permutation.
                    in_selection[i] = false;
                }
            }
        }
    }

    // Return n!
    private long Factorial(long n)
    {
        long result = 1;
        for (int i = 2; i <= n; i++) result *= i;
        return result;
    }

    bool HasCharacterAtSamePositionAsInOriginal(string [] originalWord, List<string> anagram) //check the symbol occurs in its original location or not
    {
        for (var i = 0; i < originalWord.Length; i++)
        {
            if (originalWord[i].Equals(anagram[i]))
            {
                return true;
            }
        }
        return false;
    }

0 个答案:

没有答案