检查文本文件中是否包含字符排列 [英] Check if permutations of characters are contained in text file

查看:81
本文介绍了检查文本文件中是否包含字符排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问. 我正在编写一个程序,以查找输入到应用程序中的一组字符的所有排列. 这部分工作正常. 我的问题是我需要对照用作字典的文本文件检查字符的所有排列. 前任.如果我输入字符TSTE,则输出givin是tset,ttse,ttes,tets,test,stte,stet,sett ... 我只想打印有效的单词,例如tset,sett,stet,test,tets.没有打印ttse,ttes,stte的地方.

Quick question. I am writing a program to find all the permutations of a set of characters I input into the application. This part works perfectly. My problem is that I need to check all the permutations of the characters against a text file I use as a dictionary. Ex. If I input the characters TSTE the outputs givin are tset,ttse,ttes,tets,test,stte,stet,sett... I only want to print the valid words like tset,sett,stet,test,tets. where ttse,ttes,stte is not printed.

到目前为止,我的代码如下. 在过去的几天里,我一直在拼命地划桨,似乎无法找到一种方法. 请问有什么可以看到我想念的吗?

The code I have so far is as follows. I have been scracthing at the edges of my scull for the past few days and just cant seem to find a way to do it. Please if there is anything you can see that I have missed?

谢谢

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

            Console.WindowHeight = 40;
            Console.WindowWidth = 120;

            Permute p = new Permute();            
            var d = Read();
            string line;
            string str = System.IO.File.ReadAllText("Dictionary.txt");
            while ((line = Console.ReadLine()) != null)
            {                
                char[] c2 = line.ToArray();                
                p.setper(c2);
                           }
        }
        static Dictionary<string, string> Read()
        {
            var d = new Dictionary<string, string>();
            using (StreamReader sr = new StreamReader("Dictionary.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string a = Alphabet(line);
                    string v;
                    if (d.TryGetValue(a, out v))
                    {
                        d[a] = v + "," + line;
                    }
                    else
                    {
                        d.Add(a, line);
                    }
                }
            }
            return d;
        }
        static string Alphabet(string s)
        {
            char[] a = s.ToCharArray();
            Array.Sort(a);
            return new string(a);
        }
        static void Show(Dictionary<string, string> d, string w)
        {
            string v;
            if (d.TryGetValue(Alphabet(w), out v))
            {
                Console.WriteLine(v);
            }
            else
            {
                Console.WriteLine("-----------");
            }
        }
    }
    class Permute
    {
        private void swap(ref char a, ref char b)
        {
            if (a == b) return;
            a ^= b;
            b ^= a;
            a ^= b;
        }
        public void setper(char[] list)
        {
            int x = list.Length - 1;
            go(list, 0, x);
        }
        public void go(char[] list1, int k, int m)
        {
            if (k == m)
            {

                Console.WriteLine(list1);
                Console.WriteLine(" ");

            }
            else
            {
                for (int i = k; i <= m; i++)
                {
                    swap(ref list1[k], ref list1[i]);
                    go(list1, k + 1, m);
                    swap(ref list1[k], ref list1[i]);
                }
            }
        }

推荐答案

感谢您的所有反馈. 我终于弄清楚了,找到了解决我所遇到问题的方法. 静态void Main(string [] args) { Console.BufferHeight = Int16.MaxValue-1;

Thank you for all your feedback. I have finally figured it out and found a solution to the problem I was having. 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]);
            }
        }


    }

这篇关于检查文本文件中是否包含字符排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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