Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define ll long long
  5.  
  6. string a[25];
  7. int n;
  8.  
  9. int l_dist(const string& s1, const string& s2) {
  10.     int M = s1.size() + 1;
  11.     int N = s2.size() + 1;
  12.     int D[M][N];
  13.     D[0][0] = 0;
  14.     for (int j = 1; j < N; j++) {
  15.         D[0][j] = D[0][j- 1] + 1;
  16.     }
  17.     for (int i = 1; i < M; i++) {
  18.         D[i][0] = D[(i - 1)][0] + 1;
  19.         for (int j = 1; j < N; j++) {
  20.             D[i][j] = min(min(D[i][j - 1] + 1, D[(i - 1)][j] + 1),
  21.                 D[(i - 1)][j - 1] + ((s1[i - 1] == s2[j - 1]) ? 0 : 2));
  22.         }
  23.     }
  24.     return D[(M - 1)][N - 1];
  25. };
  26.  
  27. // int l_dist(const string& s1, const string& s2) {
  28. //     int M = s1.size() + 1;
  29. //     int N = s2.size() + 1;
  30. //     int D[M][N];
  31. //     D[0][0] = 0;
  32. //     for (int j = 1; j < N; j++) {
  33. //         D[0][j] = D[0][j- 1] + 1;
  34. //     }
  35. //     for (int i = 1; i < M; i++) {
  36. //         D[i][0] = D[i - 1][0] + 1;
  37. //         for (int j = 1; j < N; j++) {
  38. //             std::vector<int> tmp = {
  39. //                 D[i - 1][j] + 1,
  40. //                 D[i][j - 1] + 1,
  41. //                 D[i - 1][j - 1] + ((s1[i - 1] == s2[j - 1]) ? 0 : 2)
  42. //             };
  43. //             D[i][j] = *min_element(tmp.begin(), tmp.end());
  44. //         }
  45. //     }
  46. //     return D[M - 1][N - 1];
  47. // };
  48.  
  49. template<typename T>
  50. typename T::size_type GeneralizedLevenshteinDistance(const T &source,
  51.                                                      const T &target,
  52.                                                      typename T::size_type insert_cost = 1,
  53.                                                      typename T::size_type delete_cost = 1,
  54.                                                      typename T::size_type replace_cost = 2) {
  55.     if (source.size() > target.size()) {
  56.         return GeneralizedLevenshteinDistance(target, source, delete_cost, insert_cost, replace_cost);
  57.     }
  58.  
  59.     using TSizeType = typename T::size_type;
  60.     const TSizeType min_size = source.size(), max_size = target.size();
  61.     std::vector<TSizeType> lev_dist(min_size + 1);
  62.  
  63.     lev_dist[0] = 0;
  64.     for (TSizeType i = 1; i <= min_size; ++i) {
  65.         lev_dist[i] = lev_dist[i - 1] + delete_cost;
  66.     }
  67.  
  68.     for (TSizeType j = 1; j <= max_size; ++j) {
  69.         TSizeType previous_diagonal = lev_dist[0], previous_diagonal_save;
  70.         lev_dist[0] += insert_cost;
  71.  
  72.         for (TSizeType i = 1; i <= min_size; ++i) {
  73.             previous_diagonal_save = lev_dist[i];
  74.             if (source[i - 1] == target[j - 1]) {
  75.                 lev_dist[i] = previous_diagonal;
  76.             } else {
  77.                 lev_dist[i] = std::min(std::min(lev_dist[i - 1] + delete_cost, lev_dist[i] + insert_cost), previous_diagonal + replace_cost);
  78.             }
  79.             previous_diagonal = previous_diagonal_save;
  80.         }
  81.     }
  82.  
  83.     return lev_dist[min_size];
  84. }
  85.  
  86. void gen_random(std::string& s, const int len) {
  87.     s.resize(len);
  88.     static const char alphanum[] =
  89.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  90.  
  91.  
  92.     for (int i = 0; i < len; ++i) {
  93.         s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
  94.     }
  95.  
  96.     s[len] = 0;
  97. }
  98.  
  99. int dist(string s1, string s2) {
  100.     int m = s1.size();
  101.     int n = s2.size();
  102.  
  103.     vector<int> v0(n + 1);
  104.     vector<int> v1(n + 1);
  105.  
  106.     for (int i = 0; i < n + 1; i++)
  107.         v0[i] = i;
  108.  
  109.     for (int i = 0; i < m; i++) {
  110.         v1[0] = i + 1;
  111.  
  112.         for (int j = 0; j < n; j++) {
  113.             int deletionCost = v0[j + 1] + 1;
  114.             int insertionCost = v1[j] + 1;
  115.             int substitutionCost = v0[j];
  116.             if (s1[i] != s2[j])
  117.                 substitutionCost += 2;
  118.             v1[j + 1] = min(min(deletionCost, insertionCost), substitutionCost);
  119.         }
  120.         swap(v0, v1);
  121.     }
  122.  
  123.     return v0[n];
  124. }
  125.  
  126. int main() {
  127.     ios::sync_with_stdio(0);
  128.     cin.tie(NULL);
  129.     srand(time(NULL));
  130.     freopen("input.txt", "r", stdin);
  131.     // freopen("output.txt", "w", stdout);
  132.     // ifstream in("input.txt");
  133.  
  134.     while (n < 25 and getline(cin, a[n])) n++;
  135.     n--;
  136.     int ans = -1, s1 = 0, s2 = 0;
  137.  
  138.     // for (int i = 0; i < n; i++) {
  139.     //     for (int j = 0; j < n; j++) {
  140.     //         if (i != j) {
  141.     //             int ld = l_dist(a[i], a[j]);
  142.     //             if (ans == -1 or ld < ans) {
  143.     //                 ans = ld;
  144.     //                 s1 = i + 1;
  145.     //                 s2 = j + 1;
  146.     //             }
  147.     //             // ld =Z GeneralizedLevenshteinDistance<string>(a[i], a[j]);
  148.     //             cout << i + 1 << " " << j + 1 << " " << ld << endl;
  149.     //         }
  150.     //     }
  151.     // }
  152.     // cout << s1 << " " << s2 << " " << ans << endl;
  153.     string a, b;
  154.  
  155.     for (int i = 1; i < 50; i ++) {
  156.         for (int j = 1; j < 50; j++) {
  157.             for (int k = 0; k < 10; k++) {
  158.                 gen_random(a, i);
  159.                 gen_random(b, j);
  160.                 int ans1 = l_dist(b, a);
  161.                 int ans2 = dist(b, a);
  162.                 if (ans1 != ans2) {
  163.                     std::cout << a << std::endl;
  164.                     std::cout << b << std::endl;
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170.     // cout << l_dist("a", "aa");
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement