The implementation of the Word program and the implementation of the Word program
I recently read "programming Pearl", which is very interesting and amazed at the clever thinking of the author. When I had nothing to do, I wanted to start a blog trainer. The content is from the book, so I simply implemented it using C. I am a simple programmer. I wrote a blog for the first time. I can't write too good code. I hope you can see a lot of me .. If you have any mistakes or comments, you can give me a chance to learn.
The content in the book is roughly as follows: Give an English dictionary and find a set of all the modified words. For example, "pots", "stop", and "tops" are different words, because each word can be obtained by changing the order of words.
Because there is no dictionary in his hand, he creates a dictionary with a random combination of 24 letters (not a real word, 3-10 letters in a random combination ). It is a line of words in the txt file.
Public static string [] Letter = {"a", "B", "c", "d", "e", "f", "g", "h ", "I", "j", "k", "l ",
"M", "n", "o", "p", "q", "r", "s", "t", "u", "v ", "w", "x", "y", "z "};
/// <Summary> /// create a dictionary // </summary> /// <param name = "count"> Number of words to be created </param> public static void CreateDictionary (int count) {using (FileStream fs = new FileStream (Path, FileMode. create, FileAccess. write) {using (StreamWriter sw = new StreamWriter (fs, Encoding. UTF8) {Random r = new Random (); StringBuilder sb = new StringBuilder (); for (int k = 0; k <count; k ++) {sb. append (CreateWord (r); sb. append ("\ r \ n");} string words = sb. toString (); sw. write (words );}}} /// <summary> // simulate the creation of a word to generate a dictionary // </summary> /// <param name = "r"> </param> // <returns> </returns> public static string CreateWord (Random r) {string word = ""; // first select a 4-to 10-bit int bit = r. next (3, 10); for (int I = 0; I <bit; I ++) {word + = Letter [r. next (0, Letter. length-1)];} return word ;}
Then, the implementation of the keyword query is achieved. Sort the letters in each word first, and then compare whether the words are the same. Sort by bubble, and then extract the modified words to implement write is really not a good way, it is really stupid, so the two arrays have to traverse one side to find.
/// <Summary> /// start searching // </summary> public static string Search (string filePath) {if (! File. Exists (filePath) {return "the File does not exist! ";} Stopwatch watch = new Stopwatch (); watch. start (); // Start timing string printfPath = filePath. substring (0, filePath. lastIndexOf ('\') + "\ output.txt"; // output path using (StreamReader sr = new StreamReader (filePath, Encoding. UTF8) {string allWords = sr. readToEnd (); string [] wordArrary = System. text. regularExpressions. regex. split (allWords, "\ r \ n", System. text. regularExpressions. regexOptions. none); // used to receive sorted messages The word string [] sortArray = new string [wordArrary. length]; // 1. first, sort each word in char [] array; // avoid creating too many variables char temp; for (int k = 0; k <wordArrary. length; k ++) {array = wordArrary [k]. toArray (); // sort each letter of a word by bubble for (int I = 0; I <array. length; I ++) {for (int j = I + 1; j <array. length; j ++) {if (array [I]> array [j]) {temp = array [j]; array [j] = array [I]; array [I] = temp ;}}sortarray [k] = new string (array ); // Store the new array} // 2. determine whether the array elements are the same. If they are the same, the output is using (StreamWriter sw = new StreamWriter (printfPath, false) {int [] sign = new int [sortArray. length]; // mark 0 not queried 1 queried for (int I = 0; I <sortArray. length; I ++) {Console. writeLine ("Total" + sortArray. length + "single word ,...... querying the "+ (I + 1) +" "); if (sign [I] = 0) {sw. write (wordArrary [I]. toString () + ""); for (int j = 0; j <sortArray. length; j ++) {if (I! = J & sortArray [I] = sortArray [j]) {sw. write (wordArrary [j]. toString () + ""); sign [j] = 1 ;}} sw. writeLine () ;}}} watch. stop (); // end return "complete! "+ Watch. ElapsedMilliseconds +" ms in total. Open "+ printfPath +" View ";}
Then there is the main function:
/// <Summary> /// dictionary Path // </summary> public static string Path {get {return System. environment. currentDirectory + "\ words.txt" ;}} static void Main (string [] args) {string filePath = ""; Console. writeLine ("Create a dictionary by 1, input the dictionary path by 2, and find the modified word ...... "); ConsoleKeyInfo key = Console. readKey (); if (key. keyChar. toString () = "1") {Console. writeLine ("creating ...... "); CreateDictionary (5*10000); Console. writeLine ("the creation is complete. Any key starts to search for the displacement ...... "); filePath = Path; Console. readKey ();} else {Console. writeLine ("Enter the path ...... "); filePath = Console. readLine ();} // start searching for the Console. writeLine ("searching ...... "); Console. writeLine (Search (filePath); Console. readKey ();}
The speed is really slow... It took more than 20 seconds for 50 thousand words.