变位词程序的实现
? ? 最近在看《編程珠璣》,挺有意思,驚嘆于作者思維的巧妙。閑來無事,就想著開了博客練練手。內容都是從書上的,我就用C#簡單實現了一下。鄙人只是剛出道的小小程序猿,第一次寫博客,實在寫不出太優秀的代碼,望看到的人多多見諒哈。。有錯誤或者意見啥的隨便提,也是我學習的機會。
? ?書上的內容大致是:給定一個英語詞典,找出其中的所有變位詞集合。例如:“pots”,“stop”和“tops”互為變位詞,因為每一個單詞都可以通過改變字目的順序來得到。
? ?因為手里也沒詞典,就用24個字母隨機組合創建了一個詞典(并不是真正的單詞,字母隨機組合3-10位)。就是在txt里邊一行一個單詞
?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>/// 創建字典/// </summary>/// <param name="count">要創建的單詞數</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>/// 模擬創建單詞 生成字典/// </summary>/// <param name="r"></param>/// <returns></returns>public static string CreateWord(Random r){string word = "";//先隨機選擇一個單詞位數 4到10位int bit = r.Next(3, 10);for (int i = 0; i < bit; i++){word += Letter[r.Next(0,Letter.Length-1)];}return word;}
?然后就是變位詞查詢的實現了,先對每一個單詞中的字母進行排序,再比較單詞是否相同即可。排序用冒泡排序,然后取出變位詞那里的實現寫的實在不咋滴,實在是笨想不到好辦法,只好兩個數組都遍歷了一邊去查找。
/// <summary>/// 開始查找/// </summary>public static string Search(string filePath){if (!File.Exists(filePath)){return "文件不存在!";}Stopwatch watch = new Stopwatch();watch.Start();//開始計時string printfPath = filePath.Substring(0, filePath.LastIndexOf('\\')) + "\\output.txt";//輸出路徑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);//用來接收排序后的單詞string[] sortArray = new string[wordArrary.Length]; //1.先對每個單詞排序char[] array; //避免創建過多變量char temp ;for (int k=0;k< wordArrary.Length;k++){array= wordArrary[k].ToArray();//使用冒泡排序對單詞每個字母排序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); //存入新數組 }//2.判斷數組元素是否相同,相同則輸出using (StreamWriter sw = new StreamWriter(printfPath,false)){int[] sign = new int[sortArray.Length]; //標記 0未查詢 1已查詢for (int i = 0; i < sortArray.Length; i++){Console.WriteLine("共" + sortArray.Length + "個單 詞,......正在查詢到第" + (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();//結束return "完成!共用時" + watch.ElapsedMilliseconds +"ms。請打開" + printfPath + "查看";}?然后是main函數:
/// <summary>/// 字典路徑/// </summary>public static string Path{get{return System.Environment.CurrentDirectory + "\\words.txt";}}static void Main(string[] args){string filePath = "";Console.WriteLine("按1創建字典,按2執行輸入字典路徑并查找變位詞......");ConsoleKeyInfo key = Console.ReadKey();if (key.KeyChar.ToString() == "1"){Console.WriteLine("正在創建......");CreateDictionary(5*10000);Console.WriteLine("創建完成,任意鍵開始查找變位詞......");filePath = Path;Console.ReadKey();}else{Console.WriteLine("請輸入路徑......");filePath = Console.ReadLine();}//開始查找Console.WriteLine("正在查找......");Console.WriteLine(Search(filePath));Console.ReadKey();}?
?速度實在有點慢。。。5萬單詞就用了20多秒,望高人指點指點=-=
轉載于:https://www.cnblogs.com/zlofyao/p/7296296.html
總結
- 上一篇: DPM(Deformable Parts
- 下一篇: TortoiseGit- 创建本地新分支