aboutsummaryrefslogtreecommitdiff
path: root/03_dynamic_programming/edit_distance_1639.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /03_dynamic_programming/edit_distance_1639.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '03_dynamic_programming/edit_distance_1639.cpp')
-rw-r--r--03_dynamic_programming/edit_distance_1639.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/03_dynamic_programming/edit_distance_1639.cpp b/03_dynamic_programming/edit_distance_1639.cpp
new file mode 100644
index 0000000..0d8c6ef
--- /dev/null
+++ b/03_dynamic_programming/edit_distance_1639.cpp
@@ -0,0 +1,22 @@
1#include <algorithm>
2#include <iostream>
3#include <string>
4#include <vector>
5
6int d(const std::string& a, const std::string& b, size_t i, size_t j,
7 std::vector<std::vector<int>>& t) {
8 if (t[i][j] != -1) return t[i][j];
9 if (i == a.size()) return t[i][j] = b.size()-j;
10 if (j == b.size()) return t[i][j] = a.size()-i;
11 if (a[i] == b[j]) return t[i][j] = d(a, b, i+1, j+1, t);
12 return t[i][j] = 1+std::min(d(a, b, i+1, j+1, t),
13 std::min(d(a, b, i+1, j, t), d(a, b, i, j+1, t)));
14}
15
16int main() {
17 std::string a, b;
18 std::cin >> a >> b;
19 std::vector<std::vector<int>>
20 t(a.size()+1, std::vector<int>(b.size()+1, -1));
21 std::cout << d(a, b, 0, 0, t) << "\n";
22}

Generated with cgit - Back to sebastiano.tronto.net