cpp-algo-cases/chapter_dynamic_programming/edit_distance.cpp

137 lines
4.1 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: edit_distance.cpp
* Created Time: 2023-07-13
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><EFBFBD><E0BCAD><EFBFBD><EFBFBD><EBA3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int editDistanceDFS(string s, string t, int i, int j) {
// <20><> s <20><> t <20><>Ϊ<EFBFBD>գ<EFBFBD><D5A3>򷵻<EFBFBD> 0
if (i == 0 && j == 0)
return 0;
// <20><> s Ϊ<>գ<EFBFBD><D5A3>򷵻<EFBFBD> t <20><><EFBFBD><EFBFBD>
if (i == 0)
return j;
// <20><> t Ϊ<>գ<EFBFBD><D5A3>򷵻<EFBFBD> s <20><><EFBFBD><EFBFBD>
if (j == 0)
return i;
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
if (s[i - 1] == t[j - 1])
return editDistanceDFS(s, t, i - 1, j - 1);
// <20><><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> = <20><><EFBFBD>롢ɾ<EBA1A2><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> + 1
int insert = editDistanceDFS(s, t, i, j - 1);
int del = editDistanceDFS(s, t, i - 1, j);
int replace = editDistanceDFS(s, t, i - 1, j - 1);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD>
return min(min(insert, del), replace) + 1;
}
/* <20><EFBFBD><E0BCAD><EFBFBD><EFBFBD><EBA3BA><EFBFBD><EFBFBD><E4BBAF><EFBFBD><EFBFBD> */
int editDistanceDFSMem(string s, string t, vector<vector<int>> &mem, int i, int j) {
// <20><> s <20><> t <20><>Ϊ<EFBFBD>գ<EFBFBD><D5A3>򷵻<EFBFBD> 0
if (i == 0 && j == 0)
return 0;
// <20><> s Ϊ<>գ<EFBFBD><D5A3>򷵻<EFBFBD> t <20><><EFBFBD><EFBFBD>
if (i == 0)
return j;
// <20><> t Ϊ<>գ<EFBFBD><D5A3>򷵻<EFBFBD> s <20><><EFBFBD><EFBFBD>
if (j == 0)
return i;
// <20><><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>֮
if (mem[i][j] != -1)
return mem[i][j];
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
if (s[i - 1] == t[j - 1])
return editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// <20><><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> = <20><><EFBFBD>롢ɾ<EBA1A2><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> + 1
int insert = editDistanceDFSMem(s, t, mem, i, j - 1);
int del = editDistanceDFSMem(s, t, mem, i - 1, j);
int replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD>
mem[i][j] = min(min(insert, del), replace) + 1;
return mem[i][j];
}
/* <20><EFBFBD><E0BCAD><EFBFBD><EFBFBD><EBA3BA>̬<EFBFBD>滮 */
int editDistanceDP(string s, string t) {
int n = s.length(), m = t.length();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 1; i <= n; i++) {
dp[i][0] = i;
}
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><D0BA><EFBFBD>
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1]) {
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
dp[i][j] = dp[i - 1][j - 1];
} else {
// <20><><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> = <20><><EFBFBD>롢ɾ<EBA1A2><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> + 1
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
}
}
}
return dp[n][m];
}
/* <20><EFBFBD><E0BCAD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD>Ķ<EFBFBD>̬<EFBFBD>滮 */
int editDistanceDPComp(string s, string t) {
int n = s.length(), m = t.length();
vector<int> dp(m + 1, 0);
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>
for (int j = 1; j <= m; j++) {
dp[j] = j;
}
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 1; i <= n; i++) {
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>
int leftup = dp[0]; // <20>ݴ<EFBFBD> dp[i-1, j-1]
dp[0] = i;
// ״̬ת<CCAC>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int j = 1; j <= m; j++) {
int temp = dp[j];
if (s[i - 1] == t[j - 1]) {
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
dp[j] = leftup;
} else {
// <20><><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> = <20><><EFBFBD>롢ɾ<EBA1A2><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٱ༭<D9B1><E0BCAD><EFBFBD><EFBFBD> + 1
dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;
}
leftup = temp; // <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>һ<EFBFBD>ֵ<EFBFBD> dp[i-1, j-1]
}
}
return dp[m];
}
/* Driver Code */
int main() {
string s = "bag";
string t = "pack";
int n = s.length(), m = t.length();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int res = editDistanceDFS(s, t, n, m);
cout << "<EFBFBD><EFBFBD> " << s << " <20><><EFBFBD><EFBFBD>Ϊ " << t << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>" << res << " <20><>\n";
// <20><><EFBFBD><EFBFBD><E4BBAF><EFBFBD><EFBFBD>
vector<vector<int>> mem(n + 1, vector<int>(m + 1, -1));
res = editDistanceDFSMem(s, t, mem, n, m);
cout << "<EFBFBD><EFBFBD> " << s << " <20><><EFBFBD><EFBFBD>Ϊ " << t << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>" << res << " <20><>\n";
// <20><>̬<EFBFBD>
res = editDistanceDP(s, t);
cout << "<EFBFBD><EFBFBD> " << s << " <20><><EFBFBD><EFBFBD>Ϊ " << t << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>" << res << " <20><>\n";
// <20>ռ<EFBFBD><D5BC>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD>Ķ<EFBFBD>̬<EFBFBD>
res = editDistanceDPComp(s, t);
cout << "<EFBFBD><EFBFBD> " << s << " <20><><EFBFBD><EFBFBD>Ϊ " << t << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>" << res << " <20><>\n";
return 0;
}