Advertisement
tien_noob

LCS Medium

Jul 10th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. //Make CSP great again
  2. #include <bits/stdc++.h>
  3. #define TASK "TESTCODE"
  4. using namespace std;
  5. const int N = 1e6 + 1, M = 5e3 + 1;
  6. string s, t;
  7. int n, m, Next[N + 1][26], f[M + 1];
  8. void read()
  9. {
  10.    cin >> s >> t;
  11.    n = s.length(); m = t.length();
  12.    s = '?' + s;
  13.    t = '?' + t;
  14. }
  15. void solve()
  16. {
  17.    for (int i = n + 1; i > 0; -- i)
  18.    {
  19.        for (int j = 0; j < 26; ++ j)
  20.        {
  21.            if (i == n + 1)
  22.            {
  23.                Next[i][j] = n + 1;
  24.            }
  25.            else
  26.            {
  27.                Next[i][j] = Next[i + 1][j];
  28.            }
  29.        }
  30.        Next[i][s[i] - 'a'] = i;
  31.    }
  32.    fill(f + 1, f + m + 1, n + 1);
  33.    for (int i = 1; i <= m; ++ i)
  34.    {
  35.        for (int j = i; j >= 1; -- j)
  36.        {
  37.            if (f[j - 1] == n + 1)
  38.            {
  39.                continue;
  40.            }
  41.            f[j] = min(f[j], Next[f[j - 1] + 1][t[i] - 'a']);
  42.        }
  43.    }
  44.    for (int i = m; i >= 0; -- i)
  45.    {
  46.        if (f[i] < n + 1)
  47.        {
  48.            cout << i; return ;
  49.        }
  50.    }
  51. }
  52. int main()
  53. {
  54.     ios_base::sync_with_stdio(false);
  55.     cin.tie(nullptr);
  56.     //freopen(TASK".INP", "r", stdin);
  57.     //freopen(TASK".OUT", "w", stdout);
  58.     int t = 1;
  59.     bool typetest = false;
  60.     if (typetest)
  61.     {
  62.         cin >> t;
  63.     }
  64.     for (int __ = 1; __ <= t; ++ __)
  65.     {
  66.         read();
  67.         solve();
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement