-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuncs.cpp
More file actions
52 lines (43 loc) · 1007 Bytes
/
Funcs.cpp
File metadata and controls
52 lines (43 loc) · 1007 Bytes
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
#include "Funcs.hpp"
#define NOMINMAX
#include <Windows.h>
#include <memory>
#include <vector>
std::string ws2s(const std::wstring& wstr)
{
int wLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), nullptr, 0, nullptr, nullptr);
std::string result(wLen, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), &result[0], wLen, nullptr, nullptr);
return result;
}
int EditDistance(const std::string a, const std::string b)
{
if(a.empty()) return b.length();
if(b.empty()) return a.length();
auto m = a.length();
auto n = b.length();
std::vector<std::vector<int>> mat(m + 1, std::vector<int>(n + 1));
for(int i = 0; i <= m; ++i)
{
mat[i][0] = i;
}
for(int j = 0; j <= n; ++j)
{
mat[0][j] = j;
}
for(int i = 1; i <= m; ++i)
{
for(int j = 1; j <= n; ++j)
{
if(a[i] == b[j])
mat[i][j] = mat[i - 1][j - 1];
else
mat[i][j] = 1 + std::min({
mat[i - 1][j],
mat[i][j - 1],
mat[i - 1][ j - 1]
});
}
}
return mat[m][n];
}