Dang_Quan_10_Tin

SPLIT

Sep 3rd, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #define task "SPLIT"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7.  
  8. const int N = 1e3 + 2;
  9. int m, n, k;
  10. const int Inf = 1e9 + 7;
  11. string s, p;
  12. int dp[N][N][11];
  13. int d[N][N][11];
  14.  
  15. void Read()
  16. {
  17.     cin >> m >> n >> k;
  18.     cin >> s >> p;
  19.     s = " " + s;
  20.     p = " " + p;
  21. }
  22.  
  23. void On3k()
  24. {
  25.     for (int i = 1; i <= m; ++i)
  26.         for (int j = 1; j <= n; ++j)
  27.         {
  28.             if (s[i] == p[j])
  29.             {
  30.                 for (int t = 1; t <= min(i, j); ++t)
  31.                     if (s[i - t + 1] == p[j - t + 1])
  32.                     {
  33.                         for (int h = 1; h <= k; ++h)
  34.                             if (dp[i - 1][j - 1][h - 1] > 0 || h == 1)
  35.                                 dp[i][j][h] = max(dp[i][j][h], dp[i - t][j - t][h - 1] + t);
  36.                     }
  37.                     else
  38.                         break;
  39.             }
  40.             else
  41.             {
  42.                 for (int h = 1; h <= k; ++h)
  43.                     dp[i][j][h] = max(dp[i - 1][j][h], dp[i][j - 1][h]);
  44.             }
  45.         }
  46.     cout << (dp[m][n][k] == 0 ? -1 : dp[m][n][k]);
  47. }
  48.  
  49. void Solve()
  50. {
  51.     fill_n(&d[0][0][0], N * N * 11, -Inf);
  52.     fill_n(&dp[0][0][0], N * N * 11, -1);
  53.     for (int i = 1; i <= m; ++i)
  54.         d[i][0][0] = -i;
  55.     for (int i = 0; i <= n; ++i)
  56.         d[0][i][0] = 0;
  57.     for (int i = 1; i <= m; ++i)
  58.         for (int j = 1; j <= n; ++j)
  59.         {
  60.             d[i][j][0] = -i;
  61.             if (s[i] == p[j])
  62.             {
  63.                 for (int t = k; t; --t)
  64.                 {
  65.                     if (d[i - 1][j - 1][t - 1] == -Inf)
  66.                         continue;
  67.                     dp[i][j][t] = max(dp[i][j][t], d[i - 1][j - 1][t - 1] + i);
  68.                     d[i][j][t] = max(d[i - 1][j - 1][t], dp[i][j][t] - i);
  69.                 }
  70.                 d[i][j][0] = d[i - 1][j - 1][0];
  71.             }
  72.             else
  73.             {
  74.                 for (int h = 1; h <= k; ++h)
  75.                 {
  76.                     dp[i][j][h] = max(dp[i - 1][j][h], dp[i][j - 1][h]);
  77.                     if (dp[i][j][h] != 0)
  78.                         d[i][j][h] = dp[i][j][h] - i;
  79.                 }
  80.             }
  81.         }
  82.     cout << dp[m][n][k];
  83. }
  84.  
  85. int32_t main()
  86. {
  87.     ios::sync_with_stdio(0);
  88.     cin.tie(0);
  89.     cout.tie(0);
  90.     if (fopen(task ".INP", "r"))
  91.         freopen(task ".INP", "r", stdin),
  92.             freopen(task ".OUT", "w", stdout);
  93.     Read();
  94.     Solve();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment