제출 #1294725

#제출 시각아이디문제언어결과실행 시간메모리
1294725DaciejMaciejNecklace (Subtask 4) (BOI19_necklace4)C++20
15 / 15
176 ms576 KiB
#include <bits/stdc++.h>

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

template <class T>

using VV = vector<vector<T>>;

using VI = vector<int>;
using VVI = vector<vector<int>>;

using VL = vector<long long>;
using VVL = vector<vector<long long>>;

using VC = vector<char>;
using VVC = vector<vector<char>>;

using VB = vector<bool>;
using VVB = vector<vector<bool>>;

using VD = vector<double>;
using VVD = vector<vector<double>>;

using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;

using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;

#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sq(a) ((a) * (a))
#define sz(x) ((int)x.size())

#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr)
#define debug(x) cerr << #x << " = " << x << endl;

const ll MOD = 1e9 + 7;

template <class T>
inline void maxi(T &a, T b)
{
    a = max(a, b);
}

template <class T>
inline void mini(T &a, T b)
{
    a = min(a, b);
}

template <class T>
inline void addi(T &a, T b)
{
    a = (a + b) % MOD;
}

template <class T>
inline void subi(T &a, T b)
{
    a = (a - b + MOD) % MOD;
}

template <class T>
inline T add(T a, T b)
{
    return (a + b) % MOD;
}

template <class T>
inline T sub(T a, T b)
{
    return (a - b + MOD) % MOD;
}

template <class T>
inline T mul(T a, T b)
{
    return ((a % MOD) * (b % MOD)) % MOD;
}

constexpr ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }

    return res;
}

const int INF = 1e9;

const int MAX_KMP = 6003;
VI p1(MAX_KMP), p2(MAX_KMP);

void recalc(string s, VI &pi)
{
    pi[0] = 0;
    FOR(i, 1, sz(s))
    {
        int j = pi[i - 1];
        while (j > 0 && s[i] != s[j])
        {
            j = pi[j - 1];
        }

        if (s[i] == s[j])
            j++;
        pi[i] = j;
    }
}

tuple<int, int, int> solve(string &s, string &t)
{
    int n = sz(s);
    int m = sz(t);

    tuple<int, int, int> ans{0, 0, 0};
    FOR(i, 0, n)
    {
        string s1 = s.substr(0, i + 1), // [..., i]
            s2 = s.substr(i + 1);       // [i+1, ...]
        reverse(ALL(s1));

        string t1 = t, t2 = t;
        reverse(ALL(t2));

        recalc(s1 + "#" + t2, p1);
        recalc(s2 + "#" + t1, p2);

        // means that we matched [j, ...] in T2 to S1
        FOR(j, 0, m)
        {
            // // to # -> to 0 -> to j'th
            // if (i == 4)
            // {
            //     // string t3 = s1 + "#" + t2;
            //     // debug(t3);
            //     debug(p1[i + 2 + m - 1 - j]);

            //     // debug(j);
            //     debug(j);

            //     if (j == 4)
            //     {
            //         debug(s2 + "#" + t1);
            //         debug(sz(s2) + 1 + j - 1);
            //         debug(p2[sz(s2) + 1 + j - 1]);
            //     }
            // }

            int id = (m - 1 - j);
            int k1 = p1[(i + 1) + 1 + id];
            int k2 = (j > 0 ? p2[sz(s2) + 1 + j - 1] : 0);

            maxi(ans, {k1 + k2, i - k1 + 1, j - k2});
        }
    }

    return ans;
}

void solve()
{
    string s, t;
    cin >> s >> t;

    auto v1 = solve(s, t);
    reverse(ALL(t));
    auto v2 = solve(s, t);
    tuple<int, int, int> ans = v1;
    if (v2 > v1)
    {
        ans = v2;
        int k = get<0>(ans) - 1;

        get<2>(ans) = sz(t) - 1 - (get<2>(ans) + k);
    }

    cout << get<0>(ans) << LINE;
    cout << get<1>(ans) << SPACE << get<2>(ans) << LINE;
}

int main()
{
    fastio();

    int t = 1;
    // cin >> t;

    while (t--)
        solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...