最近在看《编程珠玑》,挺有意思,惊叹于作者思维的巧妙。闲来无事,就想着开了博客练练手。内容都是从书上的,我就用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多秒,望高人指点指点=-=