Submission #403117

# Submission time Handle Problem Language Result Execution time Memory
403117 2021-05-12T19:22:15 Z rama_pang Necklace (Subtask 1-3) (BOI19_necklace1) C++17
85 / 85
489 ms 106460 KB
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  string S, T;
  cin >> S >> T;

  const auto Solve = [&]() -> array<int, 3> {
    int N = S.size();
    int M = T.size();

    array<int, 3> ans = {0, 0, 0};

    // lcs[i][j] = longest common suffix of S[0...i], T[0...j]
    // dp1[i][j] = longest suffix of S[0...i] which is prefix of T[j...M]
    // dp2[i][j] = longest prefix of S[i...N] which is suffix of T[0...j]

    vector<vector<int>> lcs(N, vector<int>(M));
    vector<vector<int>> dp1(N, vector<int>(M));
    vector<vector<int>> dp2(N, vector<int>(M));

    for (int s = 0; s < N; s++) {
      for (int t = 0; t < M; t++) {
        lcs[s][t] = S[s] == T[t] ? ((s > 0 && t > 0 ? lcs[s - 1][t - 1] : 0) + 1) : 0;
      }
    }

    for (int s = 0; s < N; s++) {
      for (int t = 0; t < M; t++) {
        if (lcs[s][t] > 0) {
          dp1[s][t - lcs[s][t] + 1] = max(dp1[s][t - lcs[s][t] + 1], lcs[s][t]);
          dp2[s - lcs[s][t] + 1][t] = max(dp2[s - lcs[s][t] + 1][t], lcs[s][t]);
        }
      }
    }

    for (int s = 0; s < N; s++) {
      for (int t = 0; t < M; t++) {
        if (t > 0) dp1[s][t] = max(dp1[s][t], dp1[s][t - 1] - 1);
        if (s > 0) dp2[s][t] = max(dp2[s][t], dp2[s - 1][t] - 1);
      }
    }

    for (int s = 0; s < N; s++) {
      for (int t = 0; t < M; t++) {
        int k = dp1[s][t] + (s + 1 < N && t > 0 ? dp2[s + 1][t - 1] : 0);
        if (k > 0) {
          ans = max(ans, {k, s - dp1[s][t] + 1, t + dp1[s][t] - k});
        }
      }
    }

    return ans;
  };

  auto ans1 = Solve();
  reverse(begin(T), end(T));
  auto ans2 = Solve();
  ans2[2] = int(T.size()) - ans2[2] - ans2[0];
  auto ans = max(ans1, ans2);

  cout << ans[0] << '\n' << ans[1] << ' ' << ans[2] << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 9 ms 2244 KB Output is correct
7 Correct 7 ms 2124 KB Output is correct
8 Correct 8 ms 1908 KB Output is correct
9 Correct 8 ms 2124 KB Output is correct
10 Correct 7 ms 2124 KB Output is correct
11 Correct 7 ms 2164 KB Output is correct
12 Correct 8 ms 2012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 9 ms 2244 KB Output is correct
7 Correct 7 ms 2124 KB Output is correct
8 Correct 8 ms 1908 KB Output is correct
9 Correct 8 ms 2124 KB Output is correct
10 Correct 7 ms 2124 KB Output is correct
11 Correct 7 ms 2164 KB Output is correct
12 Correct 8 ms 2012 KB Output is correct
13 Correct 458 ms 106344 KB Output is correct
14 Correct 391 ms 106460 KB Output is correct
15 Correct 489 ms 100164 KB Output is correct
16 Correct 414 ms 104588 KB Output is correct
17 Correct 333 ms 102580 KB Output is correct
18 Correct 380 ms 105460 KB Output is correct
19 Correct 386 ms 105468 KB Output is correct
20 Correct 422 ms 102508 KB Output is correct
21 Correct 438 ms 103796 KB Output is correct