Advertisement
Guest User

SearchKMP

a guest
Apr 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. public void DoSearchKMP(string text, string word)
  2.         {
  3.             int[] d = new int[word.Length];
  4.             int i = 0;
  5.             int j = -1;
  6.             d[0] = -1;
  7.             while (i < word.Length - 1)
  8.             {
  9.                 while ((j >= 0) && (word[j] != word[i]))
  10.                     j = d[j];
  11.                 i++;
  12.                 j++;
  13.                 if (word[i] == word[j])
  14.                     d[i] = d[j];
  15.                 else
  16.                     d[i] = j;
  17.             }
  18.             if (word.Length > text.Length) { Console.WriteLine("Слово \"{0}\" вiдсутнє у рядку \"{1}\"", word, text); return; }
  19.             while (i < text.Length)
  20.             {
  21.                 for (i = 0, j = 0; (i < text.Length) && (j < word.Length); i++, j++)
  22.                     while ((j >= 0) && (word[j] != text[i]))
  23.                         j = d[j];
  24.                 if (j == word.Length)
  25.                 {
  26.                     Console.WriteLine("Слово \"{0}\" знаходиться у рядку \"{1}\" за iндексом {2} ", word, text, i - j);
  27.                     return;
  28.                 }
  29.             }
  30.             Console.WriteLine("Слово \"{0}\" вiдсутнє у рядку \"{1}\"", word, text);
  31.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement