Submission #875469

# Submission time Handle Problem Language Result Execution time Memory
875469 2023-11-19T19:50:16 Z Dr_rabi3 Necklace (Subtask 4) (BOI19_necklace4) C++14
0 / 15
1500 ms 65536 KB
/***   Coding for Fun ^^ وَمَا تَوْفِيقِي إِلَّا بِاللَّهِ ۚ عَلَيْهِ تَوَكَّلْتُ وَإِلَيْهِ أُنِيبُ    ***/
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned ll
#define ld long double
#define ed '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fixed(x) fixed<<setprecision(x)
#define memo(dp,x) memset(dp,x,sizeof(dp))
#define sumOf(a) (ll)((-1 + sqrt(8LL * a + 1)) / 2)
#define debug cerr
#define Good_Bay return

using namespace std;
using namespace __gnu_pbds;
std::mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());

template < typename T > using ordered_set = tree< T, null_type, less_equal< T >, rb_tree_tag, tree_order_statistics_node_update >;

template < typename T > istream& operator >> (istream& in, vector < T >& v) {
    for (auto& x : v) in >> x;
    return in;
}

template < typename T > ostream& operator << (ostream& out, const vector < T >& v) {
    for (const T& x : v) out << x;
    return out;
}


void Accept() { // for VS code
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #else
    // freopen("darwin.in", "r", stdin);
    // freopen("visitfj.out", "w", stdout);
// #endif
}

constexpr int mod = 1e9 + 7, oo = 0x3f3f3f3f, N = 1e6 + 5;
const double pi = acos(-1), EPS = 1e-7;
constexpr ll OO = 0x3f3f3f3f3f3f3f3f;

constexpr int dy[] = { 0 , 1 , -1 , 0  , 1 , -1 , 1  , -1 };
constexpr int dx[] = { 1 , 0 ,  0 , -1 , 1 , -1 , -1 ,  1 };

int n, m;
tuple<int, int, int> ans;
string s, t, t2;

namespace str {
    // Computes the Z-array of s
    vector<int> Z(const string& s) {
        int n = (int)s.size();
        vector<int> z_S(n);
        z_S[0] = n;
        for (int i = 1, l = 0, r = 0; i < n; i++) {
            if (i <= r) { z_S[i] = min(z_S[i - l], r - i + 1); }
            while (i + z_S[i] < n && s[z_S[i]] == s[i + z_S[i]]) { z_S[i]++; }
            if (i + z_S[i] - 1 > r) {
                l = i;
                r = i + z_S[i] - 1;
            }
        }
        return z_S;
    }
    // Computes the KMP of s 
    vector<int>KMP(const string& s) {
        int n = s.size();
        vector<int>fail(n);
        for (int i = 1;i < n;i++) {
            int j = fail[i - 1];
            while (j > 0 && s[i] != s[j])j = fail[j - 1];
            if (s[i] == s[j])
                j++;
            fail[i] = j;
        }
        return fail;
    }
    // compute Longest Palindrome substring in o(n)
    string manacher(const string& p) {
        string s = "#"; for (char c : p) s += c, s += '#';
        vector<int> man(s.size());
        int mx = 0;
        for (int i = 1, l = 0, r = 1; i + 1 < s.size(); i++) {
            if (i < r) man[i] = min(r - i, man[l + r - i]);
            while (i - man[i] >= 0 && i + man[i] < s.size() && s[i - man[i]] == s[i + man[i]]) man[i]++;
            if (i + man[i] > r) l = i - man[i], r = i + man[i];
            if (man[i] - 1 > man[mx] - 1) mx = i;
        }
        string res = "";
        for (int i = mx - man[mx] + 1; i < mx + man[mx]; i++) if (s[i] != '#') res += s[i];
        return res;
    }
};


tuple<int, int, int> solve(bool rev) {
    tuple<int, int, int> ret = { 0, 0, 0 };
    for (int i = 0;i < n;++i) {
        string suf = s.substr(i, n - i), pre = s.substr(0, i);
        reverse(all(pre));
        auto kmp1 = str::KMP(suf + '#' + t);
        auto kmp2 = str::KMP(pre + '#' + t2);
        cout << suf + '#' + t << ' ' << pre + '#' + t2 << ed;
        cout << kmp1 << ' ' << kmp2 << ed;
        for (int j = 0;j < m;j++)
            ret = max(ret, { kmp1[n - i + j] + kmp2[i + m - j],
            i - kmp2[i + m - j],
            j - kmp1[n - i + j]
                });
    }
    if (rev) {
        get<1>(ret) = n - get<1>(ret) - 1 - get<0>(ret) + 1;
    }
    return ret;
}

void doWork(int T) {
    cin >> s >> t;
    n = s.size(), m = t.size();
    t2 = t;
    reverse(all(t2));
    ans = solve(0);
    reverse(all(s));
    ans = max(ans, solve(1));
    cout << get<0>(ans) << "\n" << get<1>(ans) << " " << get<2>(ans);
}

int main() {
    debug << "M3L4 El-Code MYTFHM4\n";
    Accept();
    int _ = 1;
    // cin >> _;
    for (int __ = 1;__ <= _;__++) {
        // cout << "Case " << __ << ": ";
        doWork(__);
        if (__ < _)cout << '\n';
        // cout << '\n';
    }
    Good_Bay 0;
}

Compilation message

necklace.cpp: In function 'std::string str::manacher(const string&)':
necklace.cpp:95:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for (int i = 1, l = 0, r = 1; i + 1 < s.size(); i++) {
      |                                       ~~~~~~^~~~~~~~~~
necklace.cpp:97:50: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |             while (i - man[i] >= 0 && i + man[i] < s.size() && s[i - man[i]] == s[i + man[i]]) man[i]++;
      |                                       ~~~~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 2842 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -