Submission #414750

#TimeUsernameProblemLanguageResultExecution timeMemory
414750JerryLiu06Necklace (Subtask 4) (BOI19_necklace4)C++17
15 / 15
375 ms576 KiB
#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 DP1[2 * MX], DP2[2 * 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) void genPref(int *A, string S) { FOR(i, 1, sz(S)) { int j = A[i - 1]; while (j > 0 && S[i] != S[j]) j = A[j - 1]; if (S[i] == S[j]) j++; A[i] = j; } } 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); reverse(all(L1)); string L2 = S2, R2 = S2; reverse(all(R2)); genPref(DP1, L1 + "#" + L2); genPref(DP2, R1 + "#" + R2); FOR(j, 1, M + 1) { int rInd = rev ? (M - (j + DP2[(N - i) + (M - j)])) : (j - DP1[i + j]); ans = max(ans, {DP1[i + j] + DP2[(N - i) + (M - j)], i - DP1[i + j], rInd}); } } 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 = solve(A, B, false); reverse(all(B)); ans = max(ans, solve(A, B, true)); cout << ans[0] << "\n" << ans[1] << " " << ans[2]; }

Compilation message (stderr)

necklace.cpp: In function 'void genPref(int*, std::string)':
necklace.cpp:57:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   57 |         if (S[i] == S[j]) j++; A[i] = j;
      |         ^~
necklace.cpp:57:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   57 |         if (S[i] == S[j]) j++; A[i] = j;
      |                                ^
#Verdict Execution timeMemoryGrader output
Fetching results...