# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
414707 | JerryLiu06 | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 5 ms | 612 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
const int MOD = 1e9 + 7;
const int MX = 3010;
const ll INF = 1e18;
int N, M; string A, B; int DP[MX];
// Description: For every i in [0, N), calculate res[i] be the maximum proper (not equal
// to the entire string) prefix of S[0 ... i] that is also a suffix. Complexity: O(N)
int maxPref(string S) {
F0R(i, sz(S)) DP[i] = 0;
int ans = 0; FOR(i, 1, sz(S)) {
int j = DP[i - 1]; while (j > 0 && S[i] != S[j]) j = DP[j - 1];
if (S[i] == S[j]) j++; DP[i] = j;
}
return DP[sz(S) - 1];
}
array<int, 3> solve(string S1, string S2, bool rev) {
array<int, 3> ans = {0, 0, 0}; F0R(i, N) {
string L1 = S1.substr(0, i), R1 = S1.substr(i, N);
// cout << L1 << " " << R1 << "\n";
F0R(j, M) {
string L2 = S2.substr(j, M), R2 = S2.substr(0, j);
int ansL = maxPref(L2 + "#" + L1), ansR = maxPref(R1 + "#" + R2);
if (!rev) { ans = max(ans, {ansL + ansR, i - ansL, j - ansR}); }
else { ans = max(ans, {ansL + ansR, i - ansL, M - (j + ansL)}); }
// cout << ansL + ansR << " " << i << " " << ansL << " " << j << " " << ansR << " " << (L2 + "#" + L1) << " " << (R1 + "#" + R2) << "\n";
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> A >> B; N = sz(A), M = sz(B);
array<int, 3> ans = {0, 0, 0}; ans = max(ans, solve(A, B, false));
reverse(all(B)); ans = max(ans, solve(A, B, true));
cout << ans[0] << "\n" << ans[1] << " " << ans[2] << "\n";
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |