Advertisement
AmidamaruZXC

TaskC

Nov 30th, 2020 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <set>
  4. #include <algorithm>
  5. #include <sstream>
  6. #include <iostream>
  7.  
  8.  
  9. using std::cout;
  10. using std::string;
  11. using std::cin;
  12. using std::endl;
  13. using std::vector;
  14.  
  15.  
  16. int Wagner_Fischer_for_Damerau_Levenshtein(string& s, string& t)
  17. {
  18.     int n = s.length() + 1;
  19.     int m = t.length() + 1;
  20.     int** d = new int* [n];
  21.     for (int i = 0; i < n; ++i)
  22.     {
  23.         d[i] = new int[m];
  24.         d[i][0] = i;
  25.     }
  26.     for (int j = 1; j < m; ++j)
  27.         d[0][j] = j;
  28.  
  29.     for (int i = 1; i < n; ++i)
  30.     {
  31.         for (int j = 1; j < m; ++j)
  32.         {
  33.             if (s[i - 1] == t[j - 1])
  34.                 d[i][j] = d[i - 1][j - 1];
  35.             else if (i > 1 && j > 1 && s[i - 1] == t[j - 2] && s[i - 2] == t[j - 1])
  36.                 d[i][j] = std::min(d[i - 1][j] + 1,
  37.                     std::min(d[i][j - 1] + 1,
  38.                         std::min(d[i - 1][j - 1] + 1,
  39.                             d[i - 2][j - 2] + 1)));
  40.             else
  41.                 d[i][j] = std::min(d[i - 1][j] + 1,
  42.                     std::min(d[i][j - 1] + 1,
  43.                         d[i - 1][j - 1] + 1));
  44.         }
  45.     }
  46.     int result = d[n - 1][m - 1];
  47.     for (int i = 0; i < n; ++i)
  48.         delete[] d[i];
  49.     delete[] d;
  50.     return result;
  51. }
  52.  
  53. int main()
  54. {
  55.     int n;
  56.     cin >> n;
  57.     cin.ignore();
  58.     string word, word2;
  59.     bool check = false;
  60.     string* dict = new string[n];
  61.     vector<string> res;
  62.     for (int i = 0; i < n; i++)
  63.     {
  64.         getline(cin, word);
  65.         for (int i = 0; i < word.size(); i++)
  66.         {
  67.             if (word[i] == -114)
  68.                 word[i] = word[i] + 32;
  69.             else
  70.             {
  71.                 if (word[i] <= -97 && word[i] >= -128)
  72.                     word[i] = word[i] + 80;
  73.                 if (word[i] <= 'Z' && word[i] >= 'A')
  74.                     word[i] = tolower(word[i]);
  75.             }
  76.         }
  77.         dict[i] = word;
  78.     }
  79.     while (!cin.fail())
  80.     {
  81.         check = false;
  82.         getline(cin, word);
  83.         if (word != "")
  84.         {
  85.             word2 = word;
  86.             for (int i = 0; i < word2.size(); i++)
  87.             {
  88.                 if (word2[i] == -114)
  89.                     word2[i] = word2[i] + 32;
  90.                 else
  91.                 {
  92.                     if (word2[i] <= -97 && word2[i] >= -128)
  93.                         word2[i] = word2[i] + 80;
  94.                     if (word2[i] <= 'Z' && word2[i] >= 'A')
  95.                         word2[i] = tolower(word2[i]);
  96.                 }
  97.             }
  98.             for (int i = 0; i < n; i++)
  99.                 if (dict[i] == word2)
  100.                 {
  101.                     cout << word << " - ok" << endl;
  102.                     check = true;
  103.                     break;
  104.                 }
  105.             if (!check)
  106.             {
  107.                 for (int i = 0; i < n; i++)
  108.                     if (Wagner_Fischer_for_Damerau_Levenshtein(dict[i], word2) == 1)
  109.                         res.push_back(dict[i]);
  110.                 if (res.empty())
  111.                     cout << word << " -?" << endl;
  112.                 else if (res.size() == 1)
  113.                     cout << word << " -> " << res[0] << endl;
  114.                 else
  115.                 {
  116.                     sort(res.begin(), res.end());
  117.                     cout << word << " -> ";
  118.                     for (int i = 0; i < res.size() - 1; i++)
  119.                         cout << res[i] << ", ";
  120.                     cout << res[res.size() - 1] << endl;
  121.                 }
  122.                 res.clear();
  123.             }
  124.         }
  125.     }
  126.     delete[] dict;
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement