Advertisement
AskTomorrow

Untitled

Feb 28th, 2020
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8.  
  9. int min3(int x, int y, int z)
  10. {
  11.     if (x < y && x < z)
  12.         return x;
  13.     else
  14.     if (y < z)
  15.         return y;
  16.     else
  17.         return z;
  18. }
  19.  
  20. int Wagner_Fischer(string& s, string& t, int d)
  21. {
  22.     int dist[s.length() + 1][2 * d + 1];
  23.     int j = 0;
  24.     bool flag;
  25.     for (int i = 0; i <= s.length(); ++i)
  26.     {
  27.         flag = true;
  28.         for (int k = max(0, i - d); k <= min((int)t.length(), i + d); ++k)
  29.         {
  30.             j = k - max(0, i - d);
  31.             if (min(i, k) == 0)
  32.             {
  33.                 dist[i][j] = max(i, k);
  34.                 if (dist[i][j] <= d)
  35.                 {
  36.                     flag = false;
  37.                 }
  38.                 continue;
  39.             }
  40.             if (max(0, i - d) == 0)
  41.             {
  42.                 if (s[i - 1] != t[k - 1])
  43.                     dist[i][j] = min3(dist[i - 1][j] + 1, //удаление символа из s
  44.                                       dist[i][j - 1] + 1, // вставка символа из t в s
  45.                                       dist[i - 1][j - 1] + 1); //замена символа в s на символ из t (или наоборот)
  46.                 else
  47.                     dist[i][j] = dist[i - 1][j - 1]; //просто переходим к префиксам на 1 меньше
  48.             }
  49.             else
  50.             {
  51.                 if (s[i - 1] != t[k - 1])
  52.                 {
  53.                     dist[i][j] = min(dist[i - 1][j] + 1, //удаление символа из s
  54.                                      dist[i - 1][j + 1] + 1); // вставка символа из t в s
  55.                     //замена символа в s на символ из t (или наоборот)
  56.                     if (j > 0)
  57.                     {
  58.                         dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1);
  59.                     }
  60.                 }
  61.                 else
  62.                     dist[i][j] = dist[i - 1][j]; //просто переходим к префиксам на 1 меньше
  63.             }
  64.             if (dist[i][j] <= d)
  65.             {
  66.                 flag = false;
  67.             }
  68.         }
  69.         //отсекаем заведомо неподходящие строки
  70.         if (flag)
  71.         {
  72.             return 42;
  73.         }
  74.     }
  75.  
  76.     int last_id = min((int)t.length(), (int)s.length() + d) - max(0, (int)s.length() - d);
  77.     return dist[s.length()][last_id];
  78. }
  79.  
  80. int main(int argc, const char * argv[])
  81. {
  82.     int n;
  83.     cin >> n;
  84.     vector<string> commands(n);
  85.     for (int i = 0; i < n; ++i)
  86.     {
  87.         cin >> commands[i];
  88.     }
  89.     int d;
  90.     cin >> d;
  91.     int k;
  92.     cin >> k;
  93.     vector<string> requests(k);
  94.     for (int i = 0; i < k; ++i)
  95.     {
  96.         cin >> requests[i];
  97.     }
  98.     for (int i = 0; i < k; ++i)
  99.     {
  100.         int cnt = 0;
  101.         for (int j = 0; j < n; ++j)
  102.         {
  103.             if (abs((int)(requests[i].length() - commands[j].length())) > d)
  104.             {
  105.                 continue;
  106.             }
  107.             int res;
  108.             if (requests[i].length() > commands[j].length())
  109.                 res = Wagner_Fischer(requests[i], commands[j], d);
  110.             else
  111.                 res = Wagner_Fischer(commands[j], requests[i], d);
  112.             if (res <= d)
  113.             {
  114.                 cnt++;
  115.             }
  116.         }
  117.         cout << cnt << "\n";
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement