Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include <ios>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <stack>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <utility>
  10. #include <random>
  11. #include <functional>
  12. #include <list>
  13. #include <set>
  14. #include <deque>
  15. #include <iterator>
  16. #include <map>
  17.  
  18. void get_better(std::pair<int, int>& one, std::pair<int, int> two, int diff) {
  19. two.first -= 1;
  20.  
  21. int one_diff = one.first - one.second;
  22. int two_diff = two.first - two.second;
  23.  
  24. if (one_diff != two_diff && two.first >= diff) {
  25. if (one_diff < two_diff) {
  26. one.first = two.first;
  27. one.second = two.second;
  28. }
  29. }
  30. else
  31. {
  32. if (one.first < two.first && two.first >= diff) {
  33. one.first = two.first;
  34. one.second = two.second;
  35. }
  36. }
  37. }
  38.  
  39. int main() {
  40. std::ios_base::sync_with_stdio(false);
  41.  
  42. std::string first, second;
  43. std::cin >> first >> second;
  44.  
  45. int max_k;
  46. std::cin >> max_k;
  47.  
  48. if (first.length() > second.length()) {
  49. std::swap(first, second);
  50. }
  51.  
  52. int first_length = first.length();
  53. int second_length = second.length();
  54.  
  55. if (std::abs(first_length - second_length) > max_k) {
  56. std::cout << -1 << std::endl;
  57. return 0;
  58. }
  59.  
  60. std::vector<std::pair<int, int>> cur(second_length + 1, std::make_pair(-2147483647, 0));
  61. std::vector<std::pair<int, int>> next(second_length + 1, std::make_pair(-2147483647, 0));
  62.  
  63. int skew = second_length - first_length;
  64.  
  65. cur[0].first = max_k;
  66.  
  67. for (int t_column = 1; t_column <= second_length; ++t_column) {
  68. if (cur[t_column - 1].first > std::abs(skew - t_column)) {
  69. cur[t_column].first = cur[t_column - 1].first - 1;
  70. }
  71. }
  72.  
  73. for (int row = 1; row <= first_length; ++row) {
  74. if (cur[0].first > 0) {
  75. next[0].first = cur[0].first - 1;
  76. }
  77. else if (cur[0].first <= 0)
  78. {
  79. next[0].first = -2147483647;
  80. next[0].second = 0;
  81. }
  82.  
  83. for (int column = 1; column <= second_length; ++column) {
  84. std::pair<int, int> new_value(-2147483647, 0);
  85.  
  86. if (cur[column - 1].first >= 0) {
  87. new_value.first = cur[column - 1].first;
  88. new_value.second = cur[column - 1].second;
  89. }
  90.  
  91. if (first[row - 1] != second[column - 1] && new_value.first != -2147483647) {
  92. new_value.second += 1;
  93. }
  94.  
  95. if (cur[column].first > 0) {
  96. int diff = std::abs(row + skew - column);
  97.  
  98. get_better(new_value, cur[column], diff);
  99. }
  100.  
  101. if (next[column - 1].first > 0) {
  102. int diff = std::abs(row + skew - column);
  103.  
  104. get_better(new_value, next[column - 1], diff);
  105. }
  106.  
  107. next[column] = new_value;
  108. }
  109.  
  110. cur.swap(next);
  111. }
  112.  
  113. int result = 0;
  114. if (cur[second_length].first >= 0) {
  115. result = std::max(0, cur[second_length].second - cur[second_length].first);
  116. }
  117. else
  118. {
  119. result = -1;
  120. }
  121. std::cout << result << std::endl;
  122.  
  123. std::cin >> result;
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement