aboutsummaryrefslogtreecommitdiff
path: root/03_dynamic_programming/edit_distance_1639.cpp
blob: 0d8c6ef18013ac5327bd0da98b11319450bf6ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int d(const std::string& a, const std::string& b, size_t i, size_t j,
    std::vector<std::vector<int>>& t) {
	if (t[i][j] != -1) return t[i][j];
	if (i == a.size()) return t[i][j] = b.size()-j;
	if (j == b.size()) return t[i][j] = a.size()-i;
	if (a[i] == b[j]) return t[i][j] = d(a, b, i+1, j+1, t);
	return t[i][j] = 1+std::min(d(a, b, i+1, j+1, t),
	    std::min(d(a, b, i+1, j, t), d(a, b, i, j+1, t)));
}

int main() {
	std::string a, b;
	std::cin >> a >> b;
	std::vector<std::vector<int>>
	    t(a.size()+1, std::vector<int>(b.size()+1, -1));
	std::cout << d(a, b, 0, 0, t) << "\n";
}

Generated with cgit - Back to sebastiano.tronto.net