Advertisement
StreetKatya

stroki

May 14th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Searches
  5. {
  6. public class BruteForce : ISubstringSearch
  7. {
  8. public List<int> search(string text, string searchtext)
  9. {
  10. List<int> pozition = new List<int>();
  11. int length = searchtext.Length;
  12. for (int i = 0; length <= text.Length; i++)
  13. {
  14.  
  15. if (text[i..length] == searchtext)
  16. {
  17. pozition.Add(i);
  18. }
  19. length++;
  20. }
  21. return pozition;
  22. }
  23. }
  24. public class KMP : ISubstringSearch
  25. {
  26. public List<int> search(string text, string searchtext)
  27. {
  28. int[] pf = GetPrefix(searchtext);
  29. int i = 0;
  30. int j = 0;
  31. int m = searchtext.Length;
  32. int n = text.Length;
  33. List<int> pozition = new List<int>();
  34. while (i < n)
  35. {
  36. if (text[i] == searchtext[j])
  37. {
  38. i++;
  39. j++;
  40. if (j == m)
  41. {
  42. pozition.Add(i - j);
  43.  
  44. i = i - j + 1; j = 0;
  45. }
  46. }
  47. else
  48. if (j > 0)
  49. j = pf[j - 1];
  50. else
  51. i++;
  52. }
  53.  
  54. return pozition;
  55. }
  56. private int[] GetPrefix(string s)
  57. {
  58. int[] result = new int[s.Length];
  59. result[0] = 0;
  60. int j = 0;
  61. int i = 1;
  62. while (i < s.Length)
  63. {
  64. if (s[j] == s[i])
  65. {
  66. result[i] = j + 1;
  67. i++;
  68. j++;
  69. }
  70. else
  71. if (j == 0)
  72. {
  73. result[i] = 0;
  74. i++;
  75. }
  76. else
  77. j = result[j - 1];
  78. }
  79. return result;
  80. }
  81. }
  82. public class BoyerMoore : ISubstringSearch
  83. {
  84. public List<int> search(string text, string searchtext)
  85. {
  86.  
  87. int s = 0;
  88. int m = searchtext.Length;
  89. int n = text.Length;
  90. List<int> result = new List<int>();
  91. Dictionary<char, int> bad = new Dictionary<char, int>();
  92. int[] bpos = new int[m + 1]; int[] shift = new int[m + 1];
  93.  
  94. for (int i = 0; i < m + 1; i++) shift[i] = 0;
  95.  
  96. bad = badCharHeuristic(searchtext);
  97. preprocess_strong_suffix(shift, bpos, searchtext, m);
  98.  
  99.  
  100. int j, bound = 0;
  101. for (int i = 0; i <= n - m;)
  102. {
  103. for (j = m - 1; j >= bound && searchtext[j] == text[i + j]; j--) ;
  104. if (j < bound)
  105. {
  106. result.Add(i);
  107. bound = m - shift[0];
  108. j = -1; }
  109. else
  110. {
  111. bound = 0;
  112. }
  113. if (j < bound) i += shift[j + 1];
  114. else
  115. if (j == m - 1)
  116. {
  117. if (bad.ContainsKey(text[i + j]))
  118. i += Math.Max(shift[j + 1], bad[text[i + j]]);
  119. else
  120. i += Math.Max(shift[j + 1], bad['*']);
  121. }
  122. else
  123. {
  124. if (bad.ContainsKey(text[i + j]))
  125. i += Math.Max(shift[j + 1], j - bad[text[i + j]]);
  126. else
  127. i += Math.Max(shift[j + 1], j - bad['*']);
  128. }
  129. }
  130. return result;
  131. }
  132. static void preprocess_strong_suffix(int[] shift, int[] bpos,
  133. string pat, int m)
  134. {
  135. for (int i = 0; i < m + 1; i++)
  136. {
  137. shift[i] = m;
  138. }
  139. for (int i = 0; i < m; i++)
  140. {
  141. bpos[i] = 0;
  142. }
  143. for (int j = 1, maxZidx = 0, maxZ = 0; j < m; j++)
  144. {
  145. if (j <= maxZ) bpos[j] = Math.Min(maxZ - j + 1, bpos[j - maxZidx]);
  146. while (j + bpos[j] < m && pat[m - 1 - bpos[j]] == pat[m - 1 - (j + bpos[j])]) bpos[j]++;
  147. if (j + bpos[j] - 1 > maxZ)
  148. {
  149. maxZidx = j;
  150. maxZ = j + bpos[j] - 1;
  151. }
  152. }
  153. for (int j = m - 1; j > 0; j--) shift[m - bpos[j]] = j;
  154. for (int j = 1, r = 0; j <= m - 1; j++)
  155. if (j + bpos[j] == m)
  156. for (; r <= j; r++)
  157. if (shift[r] == m) shift[r] = j;
  158.  
  159. }
  160. private Dictionary<char, int> badCharHeuristic(string searchstring)
  161. {
  162. int m = searchstring.Length;
  163. Dictionary<char, int> result = new Dictionary<char, int>();
  164. int count = 1;
  165. for (int i = m - 2; i >= 0; i--)
  166. {
  167. if (!result.ContainsKey(searchstring[i]))
  168. {
  169. result.Add(searchstring[i], count);
  170. count++;
  171. }
  172. }
  173. if (!result.ContainsKey(searchstring[m - 1]))
  174. result.Add(searchstring[m - 1], m);
  175. result.Add('*', m);
  176. return result;
  177. }
  178. }
  179.  
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement