Submission #414750

# Submission time Handle Problem Language Result Execution time Memory
414750 2021-05-31T06:30:42 Z JerryLiu06 Necklace (Subtask 4) (BOI19_necklace4) C++17
15 / 15
375 ms 576 KB
#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

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 time Memory Grader output
1 Correct 350 ms 464 KB Output is correct
2 Correct 311 ms 456 KB Output is correct
3 Correct 375 ms 420 KB Output is correct
4 Correct 297 ms 456 KB Output is correct
5 Correct 250 ms 452 KB Output is correct
6 Correct 307 ms 460 KB Output is correct
7 Correct 293 ms 452 KB Output is correct
8 Correct 317 ms 576 KB Output is correct
9 Correct 332 ms 448 KB Output is correct