Submission #706676

# Submission time Handle Problem Language Result Execution time Memory
706676 2023-03-07T10:50:49 Z LittleCube Necklace (Subtask 1-3) (BOI19_necklace1) C++14
85 / 85
718 ms 504 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

string s, t;

vector<int> Z(string s)
{
    int N = s.size(), mxZ = 0, mxi = 0;
    vector<int> z(N);
    for (int i = 1; i < N; i++)
    {
        if (i <= mxZ)
            z[i] = min(z[i - mxi], mxZ - i);
        while (i + z[i] >= mxZ && i + z[i] < N && s[i + z[i]] == s[z[i]])
        {
            z[i]++;
            mxZ = i + z[i], mxi = i;
        }
    }
    return z;
}

pair<int, pii> solve(string s, string t)
{
    int ans = 0, N = s.size(), M = t.size();
    pii sol;
    for (int i = 0; i <= N; i++)
    {
        string L, R;
        for (int j = i - 1; j >= 0; j--)
            L.push_back(s[j]);
        for (int j = M - 1; j >= 0; j--)
            L.push_back(t[j]);
        for (int j = i; j < N; j++)
            R.push_back(s[j]);
        for (int j = 0; j < M; j++)
            R.push_back(t[j]);
        vector<int> zL = Z(L), zR = Z(R);
        // cerr << t << '\n';
        // cerr << s.substr(0, i) << ' ' << s.substr(i) << '\n';
        vector<int> lL(M + 1), lR(M + 1);
        for (int j = i; j < i + M; j++)
        {
            int k = M - (j - i) - 1;
            zL[j] = min(zL[j], i);
            if(k - zL[j] + 1 < 0)
                lL[0] = max(lL[0], k + 1);
            else if(zL[j])
                lL[k - zL[j] + 1] = max(lL[k - zL[j] + 1], zL[j]);
        }
        for (int j = 1; j < M; j++)
            lL[j] = max(lL[j], lL[j - 1] - 1);

        for (int j = (N - i); j < (N - i) + M; j++)
        {
            int k = j - (N - i);
            zR[j] = min(zR[j], N - i);
            if(k + zR[j] - 1 > M)
                lR[M] = max(lR[M], M - k + 1);
            else if(zR[j])
                lR[k + zR[j] - 1] = max(lR[k + zR[j] - 1], zR[j]);
        }
        for (int j = M - 1; j >= 0; j--)
            lR[j] = max(lR[j], lR[j + 1] - 1);

        // for (int j = 0; j <= M; j++)
        //     cout << lL[j] << " \n"[j == M];
        // for (int j = 0; j <= M; j++)
        //     cout << lR[j] << " \n"[j == M];
        for (int j = 1; j <= M; j++)
            if(lL[j] + lR[j - 1] > ans)
                ans = lL[j] + lR[j - 1], sol = pii(i - lL[j], j - lR[j - 1]);
    }
    return make_pair(ans, sol);
}

signed main()
{
    // ios::sync_with_stdio(0), cin.tie(0);
    cin >> s >> t;
    pair<int, pii> ans1 = solve(s, t);
    reverse(t.begin(), t.end());
    pair<int, pii> ans2 = solve(s, t);
    ans2.S.S = t.size() - 1 - (ans2.S.S + ans2.F - 1);
    pair<int, pii> ans = max(ans1, ans2);

    cout << ans.F << '\n';
    cout << ans.S.F << " " << ans.S.S << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 15 ms 332 KB Output is correct
7 Correct 14 ms 212 KB Output is correct
8 Correct 12 ms 212 KB Output is correct
9 Correct 12 ms 212 KB Output is correct
10 Correct 12 ms 300 KB Output is correct
11 Correct 12 ms 212 KB Output is correct
12 Correct 13 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 15 ms 332 KB Output is correct
7 Correct 14 ms 212 KB Output is correct
8 Correct 12 ms 212 KB Output is correct
9 Correct 12 ms 212 KB Output is correct
10 Correct 12 ms 300 KB Output is correct
11 Correct 12 ms 212 KB Output is correct
12 Correct 13 ms 212 KB Output is correct
13 Correct 715 ms 392 KB Output is correct
14 Correct 650 ms 384 KB Output is correct
15 Correct 718 ms 376 KB Output is correct
16 Correct 675 ms 396 KB Output is correct
17 Correct 598 ms 392 KB Output is correct
18 Correct 622 ms 504 KB Output is correct
19 Correct 653 ms 388 KB Output is correct
20 Correct 672 ms 468 KB Output is correct
21 Correct 685 ms 388 KB Output is correct