Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ios>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stack>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <utility>
- #include <random>
- #include <functional>
- #include <list>
- #include <set>
- #include <deque>
- #include <iterator>
- #include <map>
- void get_better(std::pair<int, int>& one, std::pair<int, int> two, int diff) {
- two.first -= 1;
- int one_diff = one.first - one.second;
- int two_diff = two.first - two.second;
- if (one_diff != two_diff && two.first >= diff) {
- if (one_diff < two_diff) {
- one.first = two.first;
- one.second = two.second;
- }
- }
- else
- {
- if (one.first < two.first && two.first >= diff) {
- one.first = two.first;
- one.second = two.second;
- }
- }
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::string first, second;
- std::cin >> first >> second;
- int max_k;
- std::cin >> max_k;
- if (first.length() > second.length()) {
- std::swap(first, second);
- }
- int first_length = first.length();
- int second_length = second.length();
- if (std::abs(first_length - second_length) > max_k) {
- std::cout << -1 << std::endl;
- return 0;
- }
- std::vector<std::pair<int, int>> cur(second_length + 1, std::make_pair(-2147483647, 0));
- std::vector<std::pair<int, int>> next(second_length + 1, std::make_pair(-2147483647, 0));
- int skew = second_length - first_length;
- cur[0].first = max_k;
- for (int t_column = 1; t_column <= second_length; ++t_column) {
- if (cur[t_column - 1].first > std::abs(skew - t_column)) {
- cur[t_column].first = cur[t_column - 1].first - 1;
- }
- }
- for (int row = 1; row <= first_length; ++row) {
- if (cur[0].first > 0) {
- next[0].first = cur[0].first - 1;
- }
- else if (cur[0].first <= 0)
- {
- next[0].first = -2147483647;
- next[0].second = 0;
- }
- for (int column = 1; column <= second_length; ++column) {
- std::pair<int, int> new_value(-2147483647, 0);
- if (cur[column - 1].first >= 0) {
- new_value.first = cur[column - 1].first;
- new_value.second = cur[column - 1].second;
- }
- if (first[row - 1] != second[column - 1] && new_value.first != -2147483647) {
- new_value.second += 1;
- }
- if (cur[column].first > 0) {
- int diff = std::abs(row + skew - column);
- get_better(new_value, cur[column], diff);
- }
- if (next[column - 1].first > 0) {
- int diff = std::abs(row + skew - column);
- get_better(new_value, next[column - 1], diff);
- }
- next[column] = new_value;
- }
- cur.swap(next);
- }
- int result = 0;
- if (cur[second_length].first >= 0) {
- result = std::max(0, cur[second_length].second - cur[second_length].first);
- }
- else
- {
- result = -1;
- }
- std::cout << result << std::endl;
- std::cin >> result;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement