Advertisement
AskTomorrow

Untitled

Feb 28th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <vector>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. //Необходимо реализовать метод
  10. //о алгоритме можно прочитать в источниках указанных в программе курса, или на странице курса в ЛМС
  11. // или в презентациях к семинару.
  12. int Wagner_Fischer_for_Damerau_Levenshtein(string &s, string &t)
  13. {
  14.     int res = 0;
  15.     if (min(s.length(), t.length()) == 0)
  16.     {
  17.         return max(s.length(), t.length());
  18.     }
  19.     else
  20.     if (s.length() > 1 &&
  21.         t.length() > 1 &&
  22.         s[s.length() - 1] == t[t.length() - 2] &&
  23.         s[s.length() - 2] == t[t.length() - 1])
  24.     {
  25.         if (s[s.length() - 1] != t[t.length() - 1])
  26.         {
  27.             res = 1;
  28.         }
  29.         string s_1 = s.substr(0, s.length() - 1);
  30.         string s_2 = s.substr(0, s.length() - 2);
  31.         string t_1 = t.substr(0, t.length() - 1);
  32.         string t_2 = t.substr(0, t.length() - 2);
  33.         return min(min(Wagner_Fischer_for_Damerau_Levenshtein(s_1, t) + 1,
  34.                        Wagner_Fischer_for_Damerau_Levenshtein(s, t_1) + 1),
  35.                    min(Wagner_Fischer_for_Damerau_Levenshtein(s_1, t_1) + res,
  36.                        Wagner_Fischer_for_Damerau_Levenshtein(s_2, t_2) + 1));
  37.     }
  38.     else
  39.     {
  40.         if (s[s.length() - 1] != t[t.length() - 1])
  41.         {
  42.             res = 1;
  43.         }
  44.         string s_1 = s.substr(0, s.length() - 1);
  45.         string t_1 = t.substr(0, t.length() - 1);
  46.         return min(min(Wagner_Fischer_for_Damerau_Levenshtein(s_1, t) + 1,
  47.                        Wagner_Fischer_for_Damerau_Levenshtein(s, t_1) + 1),
  48.                        Wagner_Fischer_for_Damerau_Levenshtein(s_1, t_1) + res);
  49.     }
  50. }
  51.  
  52. //Не изменять метод main без крайней необходимости
  53. //ОБЯЗАТЕЛЬНО добавить в комментариях подробные пояснения и причины побудившие вас изменить код этого метода.
  54. int main(int argc, const char *argv[])
  55. {
  56.     int n;
  57.     fstream fin;
  58.     fstream fout;
  59.     fin.open("input.txt", ios::in);
  60.     fout.open("output.txt", ios::out);
  61.     if (fin.is_open())
  62.     {
  63.         string N;
  64.         getline(fin, N);
  65.         n = atoi(N.c_str());
  66.         for (int i = 0; i < n; i++)
  67.         {
  68.             string s;
  69.             string t;
  70.             getline(fin, s);
  71.             getline(fin, t);
  72.             fout << Wagner_Fischer_for_Damerau_Levenshtein(s, t) << (i == n - 1 ? "" : " ");
  73.         }
  74.         fout.close();
  75.         fin.close();
  76.     }
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement