public static void FindSmallerSequence(List <string> keywords, string filename, ref int start, ref int end) { int keywordsCount = 0; Sequence solution = new Sequence(0, int.MaxValue); List <int> kwi = new List <int>(); QueueHashtable q = new QueueHashtable(keywords); using (FileStream fs = File.OpenRead(filename)) { while (fs.Position < fs.Length) { char c = (char)fs.ReadByte(); string currentWord = ""; while (c != ' ') { currentWord += c; c = (char)fs.ReadByte(); } int i = q.IndexOf(currentWord) - currentWord.Length + 1; if (q.Kw[i].Key != currentWord) { continue; } if (!q.Kw[i].Value.Any()) { keywordsCount++; } q.Kw[i].Value.Enqueue((int)fs.Position - 1); kwi.Add(i); if (keywordsCount == keywords.Count) { Sequence newSolution = new Sequence(0, 0); while (keywordsCount == keywords.Count) { int index = kwi[0]; kwi.RemoveAt(0); newSolution.Start = q.Kw[index].Value.Dequeue(); if (!q.Kw[index].Value.Any()) { keywordsCount--; } } newSolution.End = q.Kw[kwi[kwi.Count - 1]].Value.Peek(); if (newSolution.CompareTo(solution) < 0) { solution = newSolution; Console.WriteLine(solution.Start + " - " + solution.End); Console.WriteLine("##############"); } } } } start = solution.Start; end = solution.End; }
public static void FindSmallerSequence(List<string> keywords, string filename, ref int start, ref int end) { int keywordsCount = 0; Sequence solution = new Sequence(0, int.MaxValue); List<int> kwi = new List<int>(); QueueHashtable q = new QueueHashtable(keywords); using (FileStream fs = File.OpenRead(filename)) { while (fs.Position < fs.Length) { char c = (char) fs.ReadByte(); string currentWord = ""; while (c != ' ') { currentWord += c; c = (char) fs.ReadByte(); } int i = q.IndexOf(currentWord) - currentWord.Length + 1; if (q.Kw[i].Key != currentWord) continue; if (!q.Kw[i].Value.Any()) keywordsCount++; q.Kw[i].Value.Enqueue((int) fs.Position - 1); kwi.Add(i); if (keywordsCount == keywords.Count) { Sequence newSolution = new Sequence(0, 0); while (keywordsCount == keywords.Count) { int index = kwi[0]; kwi.RemoveAt(0); newSolution.Start = q.Kw[index].Value.Dequeue(); if (!q.Kw[index].Value.Any()) keywordsCount--; } newSolution.End = q.Kw[kwi[kwi.Count - 1]].Value.Peek(); if (newSolution.CompareTo(solution) < 0) { solution = newSolution; Console.WriteLine(solution.Start + " - " + solution.End); Console.WriteLine("##############"); } } } } start = solution.Start; end = solution.End; }
static void Main() { int b = 0; int e = 0; List <string> keywords = new List <string> { "a", "b", "c" }; foreach (string keyword in keywords) { Console.WriteLine(QueueHashtable.Hash(keyword)); } Stopwatch s = new Stopwatch(); s.Start(); Exo8.FindSmallerSequence(keywords, "toogy", ref b, ref e); s.Stop(); Console.WriteLine(b + " - " + e); Console.WriteLine("Elapsed: " + s.ElapsedMilliseconds); Console.ReadLine(); }