Submission #403117

#TimeUsernameProblemLanguageResultExecution timeMemory
403117rama_pangNecklace (Subtask 1-3) (BOI19_necklace1)C++17
85 / 85
489 ms106460 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...