Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void DoSearchKMP(string text, string word)
- {
- int[] d = new int[word.Length];
- int i = 0;
- int j = -1;
- d[0] = -1;
- while (i < word.Length - 1)
- {
- while ((j >= 0) && (word[j] != word[i]))
- j = d[j];
- i++;
- j++;
- if (word[i] == word[j])
- d[i] = d[j];
- else
- d[i] = j;
- }
- if (word.Length > text.Length) { Console.WriteLine("Слово \"{0}\" вiдсутнє у рядку \"{1}\"", word, text); return; }
- while (i < text.Length)
- {
- for (i = 0, j = 0; (i < text.Length) && (j < word.Length); i++, j++)
- while ((j >= 0) && (word[j] != text[i]))
- j = d[j];
- if (j == word.Length)
- {
- Console.WriteLine("Слово \"{0}\" знаходиться у рядку \"{1}\" за iндексом {2} ", word, text, i - j);
- return;
- }
- }
- Console.WriteLine("Слово \"{0}\" вiдсутнє у рядку \"{1}\"", word, text);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement