Advertisement
999ms

Untitled

Jan 26th, 2021
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) begin(x),end(x)
  3.  
  4. using namespace std;
  5. using ll = long long;
  6.  
  7. template<typename T> void read(T& v) { cin >> v; }
  8. template<typename T1, typename T2> void read(pair<T1, T2>& v) { cin >> v.first >> v.second; }
  9. template<typename T> void read(vector<T>& v) { for (auto& val : v) read(val);}
  10. template<typename T, typename... Args> void read(T& v, Args&... vs) { read(v), read(vs...); }
  11. template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
  12. template<typename T> using max_heap = priority_queue<T, vector<T>, less<T>>;
  13.  
  14. const int MAX_LEN = 35;
  15. int dp[MAX_LEN][MAX_LEN];
  16.  
  17. int Hemming(string& a, string& b) {
  18.     int n = a.size();
  19.     int ans = 0;
  20.     for (int i = 0; i < n; i++) {
  21.       if (a[i] != b[i]) {
  22.         ans++;
  23.       }
  24.     }
  25.     return ans;
  26. }
  27.  
  28. int Levenshtein(string& a, string& d) {
  29.   int n = a.size();
  30.   int m = d.size();
  31.   for (int i = 0; i <= n; i++) {
  32.     dp[i][0] = i;
  33.   }
  34.   for (int j = 0; j <= m; j++) {
  35.     dp[0][j] = j;
  36.   }
  37.   for (int i = 1; i <= n; i++) {
  38.     for (int j = 1; j <= m; j++) {
  39.       dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] != d[j - 1])});
  40.     }
  41.   }
  42.   return dp[n][m];
  43. }
  44.  
  45.  
  46. int DamerauLevenshtein(string& a, string& d) {
  47.   int n = a.size();
  48.   int m = d.size();
  49.  
  50.   for (int i = 0; i <= n; i++) {
  51.     dp[i][0] = i;
  52.   }
  53.   for (int j = 0; j <= m; j++) {
  54.     dp[0][j] = j;
  55.   }
  56.   for (int i = 1; i <= n; i++) {
  57.     for (int j = 1; j <= m; j++) {
  58.       dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] != d[j - 1])});
  59.       if (i > 1 && j > 1 && a[i - 1] == d[j - 2] && d[j - 1] == a[i - 2]) {
  60.         dp[i][j] = min(dp[i][j], dp[i - 2][j - 2] + 1);
  61.       }
  62.     }
  63.   }
  64.   return dp[n][m];
  65. }
  66.  
  67. int main() {
  68.   ios_base::sync_with_stdio(false);
  69.   cin.tie(nullptr);
  70.   cout.tie(nullptr);
  71.   setlocale (LC_ALL,"Rus");
  72.  
  73.   FILE* input = freopen("data_ansi.txt", "r", stdin);
  74.   string str;
  75.   map<int, vector<string>> dict;
  76.   while (getline(cin, str)) {
  77.     dict[str.size()].push_back(str);
  78.   }
  79.  
  80.   for (auto& [len, v] : dict) {
  81.     cout << len << " ";
  82.    
  83.     {
  84.         auto start = clock();
  85.         ll dlt = 0;
  86.         string pre = v[0];
  87.         int sz = v.size();
  88.         for (int i = 0; i < sz; i++) {
  89.             int value = Hemming(pre, v[i]);
  90.             dlt += value;
  91.             pre = v[i];
  92.         }
  93.         auto finish = clock();
  94.      
  95.         //cout << "Hemming: " << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " Total delta " << dlt << endl;
  96.         cout << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " " << dlt << " ";
  97.        
  98.     }
  99.     {
  100.         auto start = clock();
  101.         ll dlt = 0;
  102.         string pre = v[0];
  103.         int sz = v.size();
  104.         for (int i = 0; i < sz; i++) {
  105.             int value = Levenshtein(pre, v[i]);
  106.             dlt += value;
  107.             pre = v[i];
  108.         }
  109.         auto finish = clock();
  110.      
  111.         //cout << "Levenshtein: " << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " Total delta " << dlt << endl;
  112.         cout << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " " << dlt << " ";
  113.        
  114.     }
  115.     {
  116.         auto start = clock();
  117.         ll dlt = 0;
  118.         string pre = v[0];
  119.         int sz = v.size();
  120.         for (int i = 0; i < sz; i++) {
  121.             int value = DamerauLevenshtein(pre, v[i]);
  122.             dlt += value;
  123.             pre = v[i];
  124.         }
  125.         auto finish = clock();
  126.      
  127.         //cout << "DamerauLevenshtein: " << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " Total delta " << dlt << endl;
  128.         cout << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " " << dlt << endl;
  129.        
  130.     }
  131.  
  132.   }
  133.  
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement