Submission #875252

# Submission time Handle Problem Language Result Execution time Memory
875252 2023-11-18T20:40:08 Z vjudge1 Necklace (Subtask 4) (BOI19_necklace4) C++17
15 / 15
281 ms 572 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

const int N = 6006;
int n, m, kmp1[N], kmp2[N];
tuple<int, int, int> ans;
string s, t, t2;

void calc(string s, int *kmp) {
  kmp[sz(s)] = 0;
  foru(i, 1, sz(s)-1) {
    int j = kmp[i-1];
    while (j > 0 && s[j] != s[i]) j = kmp[j-1];
    if (s[j] == s[i]) j += 1;
    kmp[i] = j;
  }
}

tuple<int, int, int> solve(bool rev) {
  tuple<int, int, int> ret = {0, 0, 0};
  foru(i, 0, n) {
    string suf = s.substr(i, n-i), pre = s.substr(0, i);
    reverse(all(pre));
    calc(suf + '#' + t, kmp1); calc(pre + '#' + t2, kmp2);
    foru(j, 0, m) ret = max(ret, {
      kmp1[n-i+j]+kmp2[i+m-j], 
      i - kmp2[i+m-j], 
      j - kmp1[n-i+j]
    });
  }
  if (rev) {
    get<1>(ret) = n-get<1>(ret)-1-get<0>(ret)+1;
  }
  return ret;
}

int main() {
  cin >> s >> t;
  n = sz(s); m = sz(t);
  t2 = t;
  reverse(all(t2));
  ans = solve(0);
  reverse(all(s));
  ans = max(ans, solve(1));

  cout << get<0>(ans) << "\n" << get<1>(ans) << " " << get<2>(ans);
}
# Verdict Execution time Memory Grader output
1 Correct 265 ms 572 KB Output is correct
2 Correct 229 ms 560 KB Output is correct
3 Correct 281 ms 344 KB Output is correct
4 Correct 218 ms 536 KB Output is correct
5 Correct 187 ms 348 KB Output is correct
6 Correct 209 ms 348 KB Output is correct
7 Correct 211 ms 568 KB Output is correct
8 Correct 233 ms 568 KB Output is correct
9 Correct 252 ms 540 KB Output is correct