Advertisement
Korotkodul

CF

Jul 14th, 2024 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49. bool see = 0;
  50.  
  51. int lcs(vector <char> a, vector <char> b) {
  52.     int m = a.size();
  53.     vector <vector <int> > v(m, vector <int> (m, 0));
  54.     if (a[0] == b[0]) {
  55.         v[0][0] = 1;
  56.     } else if (a[1] == b[0]) {
  57.         v[1][0] = 1;
  58.     } else if (a[0] == b[1]) {
  59.         v[0][1] = 1;
  60.     }
  61.     for (int i = 1; i < m; ++i) {
  62.         for (int j = 1; j < m; ++j) {
  63.             if (a[i] == b[j]) {
  64.                 v[i][j] = v[i - 1][j - 1] + 1;
  65.             }
  66.             else if (v[i - 1][j] >= v[i][j - 1]) {
  67.                 v[i][j] = v[i - 1][j];
  68.             }
  69.             else {
  70.                 v[i][j] = v[i][j - 1];
  71.             }
  72.         }
  73.     }
  74.     if (see) {
  75.         cout << "v\n";
  76.         cvv(v);
  77.     }
  78.     return v[m - 1][m - 1];
  79. }
  80.  
  81. void slv() {
  82.     int n, q; cin >> n >> q;
  83.     string a, b; cin >> a >> b;
  84.     for (int k = 0; k < q; ++k) {
  85.         int l, r; cin >> l >> r;
  86.         l--; r--;
  87.         vector <char> sa, sb;
  88.         for (int i = l; i <= r; ++i) {
  89.             sa.push_back(a[i]);
  90.             sb.push_back(b[i]);
  91.         }
  92.         sort(sa.begin(), sa.end());
  93.         sort(sb.begin(), sb.end());
  94.         if (see) {
  95.             cout << "l r " << l << ' ' << r << "\n";
  96.             cout << "sa sb\n";
  97.             for (char l: sa) cout << l;
  98.             cout << "\n";
  99.             for (char l: sb) cout << l;
  100.             cout << "\n";
  101.         }
  102.         int cmn = lcs(sa, sb);
  103.         int m = r - l + 1;
  104.         int ans = m - cmn;
  105.         cout << ans << "\n";
  106.     }
  107. }
  108. /*
  109. 1
  110. 5 3
  111. abcde
  112. edcba
  113. 1 5
  114. 1 4
  115. 3 3
  116. */
  117.  
  118. int main() {
  119.     ios::sync_with_stdio(0);
  120.     cin.tie(0);
  121.     cout.tie(0);
  122.     int t; cin >> t;
  123.     for (int go = 0; go < t; ++go) {
  124.         slv();
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement