Advertisement
AskTomorrow

Untitled

Feb 28th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. int min3(int x, int y, int z)
  2. {
  3. if (x < y && x < z)
  4. return x;
  5. else
  6. if (y < z)
  7. return y;
  8. else
  9. return z;
  10. }
  11.  
  12. int Wagner_Fischer(string& s, string& t, int d)
  13. {
  14. int dist[s.length() + 1][2 * d + 1];
  15. int j = 0;
  16. bool flag;
  17. for (int i = 0; i <= s.length(); ++i)
  18. {
  19. flag = true;
  20. for (int k = max(0, i - d); k <= min((int)t.length(), i + d); ++k)
  21. {
  22. j = k - max(0, i - d);
  23. if (min(i, k) == 0)
  24. {
  25. dist[i][j] = max(i, k);
  26. if (dist[i][j] <= d)
  27. {
  28. flag = false;
  29. }
  30. continue;
  31. }
  32. if (max(0, i - d) == 0)
  33. {
  34. if (s[i - 1] != t[k - 1])
  35. dist[i][j] = min3(dist[i - 1][j] + 1, //удаление символа из s
  36. dist[i][j - 1] + 1, // вставка символа из t в s
  37. dist[i - 1][j - 1] + 1); //замена символа в s на символ из t (или наоборот)
  38. else
  39. dist[i][j] = dist[i - 1][j - 1]; //просто переходим к префиксам на 1 меньше
  40. }
  41. else
  42. {
  43. if (s[i - 1] != t[k - 1])
  44. {
  45. dist[i][j] = min(dist[i - 1][j] + 1, //удаление символа из s
  46. dist[i - 1][j + 1] + 1); // вставка символа из t в s
  47. //замена символа в s на символ из t (или наоборот)
  48. if (j > 0)
  49. {
  50. dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1);
  51. }
  52. }
  53. else
  54. dist[i][j] = dist[i - 1][j]; //просто переходим к префиксам на 1 меньше
  55. }
  56. if (dist[i][j] <= d)
  57. {
  58. flag = false;
  59. }
  60. }
  61. //отсекаем заведомо неподходящие строки
  62. if (flag)
  63. {
  64. return 42;
  65. }
  66. }
  67.  
  68. int last_id = min((int)t.length(), (int)s.length() + d) - max(0, (int)s.length() - d);
  69. return dist[s.length()][last_id];
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement