# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
664702 | Shin | Necklace (Subtask 4) (BOI19_necklace4) | C++14 | 261 ms | 528 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
auto build_kmp = [&](string s) {
int n = (int) s.size();
vector<int> f(n, 0);
for (int i = 1, j = 0; i < n; i ++) {
while (j > 0 && s[j] != s[i]) {
j = f[j - 1];
}
if (s[j] == s[i]) {
j ++;
}
f[i] = j;
}
return f;
};
auto calc = [&](string s, string t, bool rev) {
int n = (int) s.size();
int m = (int) t.size();
tuple<int, int, int> res(0, 0, 0);
for (int i = 0; i < n; i ++) {
string rvs = s.substr(0, i);
reverse(rvs.begin(), rvs.end());
string rvt = t;
reverse(rvt.begin(), rvt.end());
vector<int> l_p = build_kmp(rvs + "#" + t);
vector<int> r_p = build_kmp(s.substr(i, n - i) + "#" + rvt);
for (int j = 0; j < m; j ++) {
int tmp = l_p[i + j + 1] + r_p[n - i + m - j - 1];
int l = i - l_p[i + j + 1];
int r = j + 1 - l_p[i + j + 1];
if (rev) {
r = m - j - 1 - r_p[n - i + m - j - 1];
}
maximize(res, make_tuple(tmp, l, r));
}
}
return res;
};
string s, t; cin >> s >> t;
auto res = calc(s, t, 0);
reverse(t.begin(), t.end());
maximize(res, calc(s, t, 1));
auto [len, l, r] = res;
cout << len << "\n" << l << " " << r;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |