Задача такова, есть словарь на 100+ тысяч слов, нужно проверить каждое слово в нем на его анаграммы, в этом же словаре. не используя linq. Можно использовать sql ( предпочтения преподавателя) но мол лучше всего стандартными средствами. Я задачу то выполнил но выполняет он её за 2 минуты 15 секунд, хотя запрос на линку делает это за 1 секунду..
вот код(можно вставить сразу в linqpad) :
| Код | void Main() { s = st.ToList(); for (int i = 0; i < s.Count; i++) { s2.Add(chrtostr((s[i].ToCharArray().OrderBy(x => x).ToArray()))) ; } while(s.Count!=0) { ann = ScanAnn(s2[0]); if (ann.Count > 3){ for (int i = 0; i < ann.Count; i++) Console.Write(ann[i] + ' '); Console.WriteLine(); } } }
// Define other methods and classes here static string chrtostr(char[] value) { /* * * Faster way to convert char to string. * * */ return new string(value); } static List<string> s = new List<string>(); static List<string> s2 = new List<string>(); static List<string> ann = new List<string>(); static string[] st= (File.ReadAllLines("c:\\words.txt"));
static List<string> ScanAnn(string fstr) { List<string> ann = new List<string>(); int inx; do { inx = s2.IndexOf(fstr); if (inx != -1) { ann.Add(s[inx]); s.RemoveAt(inx); s2.RemoveAt(inx); } } while (inx != -1); return ann; }
|
если нужно скину словарь
|