From 1b7affe3159f2d477b44ad8964f7d31e9be8fb7e Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Tue, 14 Jun 2022 22:42:54 +0800 Subject: [PATCH] add: 220614 --- cpp/2206/220614.cpp | 34 ++++++++++++++++++++++++++++++++++ cpp/2206/CMakeLists.txt | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 cpp/2206/220614.cpp diff --git a/cpp/2206/220614.cpp b/cpp/2206/220614.cpp new file mode 100644 index 0000000..249fda1 --- /dev/null +++ b/cpp/2206/220614.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +/** + * 583. Delete Operation for Two Strings + * Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same. + * In one step, you can delete exactly one character in either string. + */ + +class Solution { +public: + static int minDistance(const std::string& word1, const std::string& word2) { + const int n1 = word1.length(), n2 = word2.length(); + int dp[501][501]; + std::memset(dp, -1, sizeof dp); + std::function d = [&](int i, int j) { + if (i < 0 || j < 0) + return 0; + if (dp[i][j] > 0) + return dp[i][j]; + if (word1[i] == word2[j]) + return dp[i][j] = 1 + d(i - 1, j - 1); + return dp[i][j] = std::max(d(i - 1, j), d(i, j - 1)); + }; + return n1 + n2 - (d(n1 - 1, n2 - 1) << 1); + } +}; + +int main() { + std::cout << Solution::minDistance("mart", "karma"); + return 0; +} diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index c7fb8a6..9b45ed7 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220614-CN.cpp) +ADD_EXECUTABLE(2206 220614.cpp)