Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <locale>
  4. #include <cstring>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. int min(int a, int b, int c) {
  10. int min = a;
  11. if (b < min) min = b;
  12. if (c < min) min = c;
  13. return min;
  14. }
  15.  
  16. int different(int a, int b) {
  17. if (a != b) return 1;
  18. else return 0;
  19. }
  20.  
  21. int main() {
  22.  
  23. setlocale(LC_ALL, "Rus");
  24.  
  25.  
  26. string S1;
  27. string S2;
  28. cin >> S1;
  29. cin >> S2;
  30.  
  31.  
  32.  
  33. size_t m = S2.size() + 1;
  34. size_t n = S1.size() + 1;
  35.  
  36. vector<vector<int>> D(m);
  37.  
  38. for (size_t i = 0; i < m; i++) D[i].resize(n);
  39. for (size_t j = 0; j < n; j++) D[0][j] = j;
  40. for (size_t k = 0; k < m; k++) D[k][0] = k;
  41.  
  42. for (size_t i = 1; i < m; i++) {
  43.  
  44. for (size_t j = 1; j < n; j++) {
  45. D[i][j] = min(D[i - 1][j - 1] + different(S2[j - 1], S1[i - 1]), D[i - 1][j] + 1, D[i][j - 1] + 1);
  46. }
  47. }
  48.  
  49. cout << endl << D[m - 1][n - 1];
  50.  
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement