Реализация алгоритма Ахо-Корасика - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте. Реализовал на с# алгоритм. Ошибок не вылетает. Но почему то, ничего не возвращает в консоли. Помогите пожалуйста.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _5
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string[] keywords = { "как", "дела", "привет" };
  13. string text = "приветкакдела";
  14. List<int> states = FindAllStates(text, keywords);
  15. }
  16. private const int MaxStates = 20;
  17. private const int MaxChars = 32;
  18. private static int[] Out = new int[MaxStates];
  19. private static int[] FF = new int[MaxStates];
  20. private static int[,] GF = new int[MaxStates, MaxChars];
  21. private static int BuildMatchingMachine(string[] words, char lowestChar = 'а', char highestChar = 'я')
  22. {
  23. Out = Enumerable.Repeat(0, Out.Length).ToArray();
  24. FF = Enumerable.Repeat(-1, FF.Length).ToArray();
  25. for (int i = 0; i < MaxStates; ++i)
  26. {
  27. for (int j = 0; j < MaxChars; ++j)
  28. {
  29. GF[i, j] = -1;
  30. }
  31. }
  32. int states = 1;
  33. for (int i = 0; i < words.Length; ++i)
  34. {
  35. string keyword = words[i];
  36. int currentState = 0;
  37. for (int j = 0; j < keyword.Length; ++j)
  38. {
  39. int c = keyword[j] - lowestChar;
  40. if (GF[currentState, c] == -1)
  41. {
  42. GF[currentState, c] = states++;
  43. }
  44. currentState = GF[currentState, c];
  45. }
  46. Out[currentState] |= (1 << i);
  47. }
  48. for (int c = 0; c < MaxChars; ++c)
  49. {
  50. if (GF[0, c] == -1)
  51. {
  52. GF[0, c] = 0;
  53. }
  54. }
  55. List<int> q = new List<int>();
  56. for (int c = 0; c <= highestChar - lowestChar; ++c)
  57. {
  58. if (GF[0, c] != -1 && GF[0, c] != 0)
  59. {
  60. FF[GF[0, c]] = 0;
  61. q.Add(GF[0, c]);
  62. }
  63. }
  64. while (Convert.ToBoolean(q.Count))
  65. {
  66. int state = q[0];
  67. q.RemoveAt(0);
  68. for (int c = 0; c <= highestChar - lowestChar; ++c)
  69. {
  70. if (GF[state, c] != -1)
  71. {
  72. int failure = FF[state];
  73. while (GF[failure, c] == -1)
  74. {
  75. failure = FF[failure];
  76. }
  77. failure = GF[failure, c];
  78. FF[GF[state, c]] = failure;
  79. Out[GF[state, c]] |= Out[failure];
  80. q.Add(GF[state, c]);
  81. }
  82. }
  83. }
  84. return states;
  85. }
  86. private static int FindNextState(int currentState, char nextInput, char lowestChar = 'а')
  87. {
  88. int answer = currentState;
  89. int c = nextInput - lowestChar;
  90. while (GF[answer, c] == -1)
  91. {
  92. answer = FF[answer];
  93. }
  94. return GF[answer, c];
  95. }
  96. public static List<int> FindAllStates(string text, string[] keywords, char lowestChar = 'а', char highestChar = 'я')
  97. {
  98. BuildMatchingMachine(keywords, lowestChar, highestChar);
  99. int currentState = 0;
  100. List<int> retVal = new List<int>();
  101. for (int i = 0; i < text.Length; ++i)
  102. {
  103. currentState = FindNextState(currentState, text[i], lowestChar);
  104. if (Out[currentState] == 0)
  105. continue;
  106. for (int j = 0; j < keywords.Length; ++j)
  107. {
  108. if (Convert.ToBoolean(Out[currentState] & (1 << j)))
  109. {
  110. retVal.Insert(0, i - keywords[j].Length + 1);
  111. }
  112. }
  113. }
  114. return retVal;
  115. }
  116. }
  117. }

Решение задачи: «Реализация алгоритма Ахо-Корасика»

textual
Листинг программы
  1.         static void Main(string[] args)
  2.         {
  3.             string[] keywords = { "как", "дела", "привет" };
  4.             string text = "приветкакдела";
  5.             List<int> states = FindAllStates(text, keywords);
  6.             foreach(var item in states)
  7.             {
  8.                 Console.WriteLine(item);
  9.             }
  10.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 3.833 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы