Repository navigation
Expand file tree
/
Copy path0072.cpp
More file actions
85 lines (71 loc) · 1.91 KB
/
Copy path0072.cpp
File metadata and controls
85 lines (71 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Edit Distance | DP-5
// https://www.geeksforgeeks.org/edit-distance-dp-5/
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
static const auto _ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
int minDistance(string word1, string word2) {
if (word1.size()==0) return word2.size();
if (word2.size()==0) return word1.size();
int m = word1.size();
int n = word2.size();
int dp[m+1][n+1]; // dp[i][j]: minDistance of word1[0:i) and word2[0:j)
for (int i=0; i<=m; ++i) dp[i][0] = i;
for (int j=0; j<=n; ++j) dp[0][j] = j;
for (int i=0; i<word1.size(); ++i) {
for (int j=0; j<word2.size(); j++) {
if (word1[i]==word2[j]) {
dp[i+1][j+1] = dp[i][j];
} else {
dp[i+1][j+1] = 1 + min(min(dp[i+1][j], dp[i][j+1]), dp[i][j]);
}
// insert: dp[m][n-1]
// remove: dp[m-1][n]
// replace: dp[m-1][n-1]
}
}
return dp[m][n];
}
};
int main() {
string sin, rin, s1, s2;
int r;
vector<int> vr;
vector<string> vs1, vs2;
ifstream fp;
fp.open("0072.txt");
while (getline(fp, sin, '\n')) {
stringstream ss(sin);
ss >> s1 >> s2;
vs1.push_back(s1);
vs2.push_back(s2);
getline(fp, rin, '\n');
stringstream sr(rin);
sr >> r;
vr.push_back(r);
}
fp.close();
printf("--- --- --- --- \n");
printf("%s\t%s\t%s\t%s\n", "T","L", "√", "?");
printf("--- --- --- --- \n");
int line=1;
for (int i=0; i<vr.size(); ++i) {
int res = minDistance(vs1[i], vs2[i]);
printf("%d\t%d\t%d\t%d\t\n", res==vr[i], line, vr[i], res);
line += 1;
}
return 0;
}