Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #define task "SPLIT"
- using namespace std;
- using ll = long long;
- using ld = long double;
- const int N = 1e3 + 2;
- int m, n, k;
- const int Inf = 1e9 + 7;
- string s, p;
- int dp[N][N][11];
- int d[N][N][11];
- void Read()
- {
- cin >> m >> n >> k;
- cin >> s >> p;
- s = " " + s;
- p = " " + p;
- }
- void On3k()
- {
- for (int i = 1; i <= m; ++i)
- for (int j = 1; j <= n; ++j)
- {
- if (s[i] == p[j])
- {
- for (int t = 1; t <= min(i, j); ++t)
- if (s[i - t + 1] == p[j - t + 1])
- {
- for (int h = 1; h <= k; ++h)
- if (dp[i - 1][j - 1][h - 1] > 0 || h == 1)
- dp[i][j][h] = max(dp[i][j][h], dp[i - t][j - t][h - 1] + t);
- }
- else
- break;
- }
- else
- {
- for (int h = 1; h <= k; ++h)
- dp[i][j][h] = max(dp[i - 1][j][h], dp[i][j - 1][h]);
- }
- }
- cout << (dp[m][n][k] == 0 ? -1 : dp[m][n][k]);
- }
- void Solve()
- {
- fill_n(&d[0][0][0], N * N * 11, -Inf);
- fill_n(&dp[0][0][0], N * N * 11, -1);
- for (int i = 1; i <= m; ++i)
- d[i][0][0] = -i;
- for (int i = 0; i <= n; ++i)
- d[0][i][0] = 0;
- for (int i = 1; i <= m; ++i)
- for (int j = 1; j <= n; ++j)
- {
- d[i][j][0] = -i;
- if (s[i] == p[j])
- {
- for (int t = k; t; --t)
- {
- if (d[i - 1][j - 1][t - 1] == -Inf)
- continue;
- dp[i][j][t] = max(dp[i][j][t], d[i - 1][j - 1][t - 1] + i);
- d[i][j][t] = max(d[i - 1][j - 1][t], dp[i][j][t] - i);
- }
- d[i][j][0] = d[i - 1][j - 1][0];
- }
- else
- {
- for (int h = 1; h <= k; ++h)
- {
- dp[i][j][h] = max(dp[i - 1][j][h], dp[i][j - 1][h]);
- if (dp[i][j][h] != 0)
- d[i][j][h] = dp[i][j][h] - i;
- }
- }
- }
- cout << dp[m][n][k];
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- freopen(task ".INP", "r", stdin),
- freopen(task ".OUT", "w", stdout);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment