Advertisement
AmidamaruZXC

Untitled

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