Advertisement
mfgnik

Untitled

Nov 20th, 2020
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. int LevenshteinDistance(const std::string &first_string, const std::string &second_string) {
  2.     std::vector<std::vector<int>> dynamic(first_string.size() + 1,
  3.                                           std::vector<int>(second_string.size() + 1));
  4.     for (int index = 0; index <= static_cast<int>(first_string.size()); ++index) {
  5.         dynamic[index][0] = index;
  6.     }
  7.     for (int index = 0; index <= static_cast<int>(second_string.size()); ++index) {
  8.         dynamic[0][index] = index;
  9.     }
  10.     for (size_t index_first = 1; index_first <= first_string.size(); ++index_first) {
  11.         for (size_t index_second = 1; index_second <= second_string.size(); ++index_second) {
  12.             if (first_string[index_first - 1] != second_string[index_second - 1]) {
  13.                 dynamic[index_first][index_second] =
  14.                         Min(1 + dynamic[index_first - 1][index_second],
  15.                             1 + dynamic[index_first][index_second - 1],
  16.                             1 + dynamic[index_first - 1][index_second - 1]);
  17.             } else {
  18.                 dynamic[index_first][index_second] = dynamic[index_first - 1][index_second - 1];
  19.             }
  20.         }
  21.     }
  22.     return dynamic.back().back();
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement