# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
664673 | Shin | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 224 ms | 448 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
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 - r_p[i + j + 1] - 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;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |