寻找重复的pernumetrations [英] Finding duplicate pernumetrations

查看:49
本文介绍了寻找重复的pernumetrations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有人知道如何检查一个单词的重复排列的方法?



即如果我输入字符TSTE其中一个排列将是SETT。我的问题是因为输出给了我SETT两次(x2),我想建立一个检查,以防止发生这种情况。这是因为在单词中使用了2个T',它将它视为一个不同的单词。



任何帮助将不胜感激。



现在看看我如何找到我的排列。

这是我的代码,直到现在:



Hey
Does anybody know of a method of how you can check for duplicate permutations of a word?

I.e if I input the characters TSTE one of the permutations will be SETT. my problem comes in on the fact that the output gives me SETT twice(x2), I want to build in a check that keeps that from happening. This is because of the use of 2 T''s in the word and it sees it as a different word.

Any help will be greatly appreciated.

For you to see how I am finding my permutations at the moment.
Here is my code until now:

static void Main(string[] args)
        {
            Console.BufferHeight = Int16.MaxValue - 1;

            Console.WindowHeight = 40;
            Console.WindowWidth = 120;
            Console.WriteLine("Enter your caracters for the anagram: ");
            //var d = Read();
            string line;
            //string DictionaryInput = System.IO.File.ReadAllText("Dictionary.txt");
            while ((line = Console.ReadLine()) != null)
            {
                Console.WriteLine("Your results are: ");
                char[] charArray = line.ToArray();
                //Show(d, line);                    //Using this to check that words found are the correct words in the dictionary.
                setper(charArray);
                Console.WriteLine("-----------------------------------------DONE-----------------------------------------");
                Console.WriteLine("Enter your caracters for the anagram: ");
                File.Delete("Permutations.txt");
            }

        }
     


        static void swap(ref char a, ref char b)
        {
            if (a == b) return;
            a ^= b;
            b ^= a;
            a ^= b;
        }
        static void setper(char[] list)
        {
            int x = list.Length - 1;
            permuteWords(list, 0, x);
        }
        static void permuteWords(char[] list1, int k, int m)
        {

            if (k == m)
            {
                StreamWriter sw = new StreamWriter("Permutations.txt", true);
                sw.WriteLine(list1);
                sw.Close();
                Regex permutationPattern = new Regex(new string(list1));
                string[] permutations = File.ReadAllLines("Permutations.txt");
                Regex pattern = new Regex(new string(list1));
                string[] lines = File.ReadAllLines("Dictionary.txt");

                foreach (string line in lines)
                {
                    var matches = pattern.Matches(line);
                    if (pattern.ToString() == line)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            else
            {
                for (int i = k; i <= m; i++)
                {
                    swap(ref list1[k], ref list1[i]);
                    permuteWords(list1, k + 1, m);
                    swap(ref list1[k], ref list1[i]);
                }
            }


        }

推荐答案

取决于你的方式真正做到了 - 有很多方法,从你在列表中添加新元素时检查它是否在你的列表中,直到使用Linq只返回不同的值。



我们真的不能给你一个一刀切的版本,但是Linq方法的版本非常简单:

Depends how you are doing it really - there are loads of ways, starting with checking if it''s in your list when you add new elements, up to using Linq to return only Distinct values.

We can''t really give you a "one-size fits all" version, but the Linq method version is very simple:
List<string> listOfPermutations = new List<string>();
listOfPermutations.Add("SETT");
listOfPermutations.Add("STET");
listOfPermutations.Add("SETT");
Console.WriteLine("Before");
foreach (string s in listOfPermutations)
    {
    Console.WriteLine(s);
    }
listOfPermutations = listOfPermutations.Distinct().ToList();
Console.WriteLine("After");
foreach (string s in listOfPermutations)
    {
    Console.WriteLine(s);
    }



输出:


Output:

Before
SETT
STET
SETT
After
SETT
STET


这篇关于寻找重复的pernumetrations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆