제출 #1344246

#제출 시각아이디문제언어결과실행 시간메모리
1344246ja_tausfPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
179 ms19056 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define vll vector<ll>
#define vpl vector<pair<long long, long long>>
#define _fastio  ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lmax LONG_LONG_MAX
#define lmin LONG_LONG_MIN
#define mxn 200007
#define endl <<'\n'
// #pragma GCC target("sse4")
// #pragma GCC target ("sse4.2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("03")
// #pragma GCC optimize("unroll loops")

ll t, n, m, k, w;
string s, s1;
// const int mod = 1e9 + 7;

#define all(x) x.begin(), x.end()
#define MAXLEN 1000010
constexpr uint64_t mod = (1ULL << 61) - 1;

const uint64_t seed = chrono::system_clock::now().time_since_epoch().count();
const uint64_t base = mt19937_64(seed)() % (mod / 3) + (mod / 3);

uint64_t base_pow[MAXLEN];

int64_t modmul(uint64_t a, uint64_t b){
    uint64_t l1 = (uint32_t)a, h1 = a >> 32, l2 = (uint32_t)b, h2 = b >> 32;
    uint64_t l = l1 * l2, m = l1 * h2 + l2 * h1, h = h1 * h2;
    uint64_t ret = (l & mod) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
    ret = (ret & mod) + (ret >> 61);
    ret = (ret & mod) + (ret >> 61);
    return ret - 1;
}

void init(){
    base_pow[0] = 1;
    for (int i = 1; i < MAXLEN; i++){
        base_pow[i] = modmul(base_pow[i - 1], base);
    }
}

struct PolyHash{
    /// Remove suff vector and usage if reverse hash is not required for more speed
    vector<int64_t> pref;

    PolyHash() {}

    template <typename T>
    PolyHash(const vector<T>& ar){
        if (!base_pow[0]) init();

        int n = ar.size();
        assert(n < MAXLEN);
        pref.resize(n + 3, 0);

        for (int i = 1; i <= n; i++){
            pref[i] = modmul(pref[i - 1], base) + ar[i - 1] + 997;
            if (pref[i] >= mod) pref[i] -= mod;
        }
    }

    PolyHash(const char* str)
        : PolyHash(vector<char> (str, str + strlen(str))) {}
    PolyHash(string str)
        : PolyHash(vector<char> (all(str))) {}
    uint64_t get_hash(int l, int r){ // 0-based indexing
        int64_t h = pref[r + 1] - modmul(base_pow[r - l + 1], pref[l]);
        return h < 0 ? h + mod : h;
    }
};

signed main()
{
    _fastio;
    ll tc = 0;
    cin >> t;
    while (t--)
    { 
        cin >> s;
        n = s.size();
        PolyHash hs(s);
        
        ll ans = 0, l1 = 0, r1 = 0, l2 = n - 1, r2 = n - 1;
        while (r1 < l2)
        {
            if (hs.get_hash(l1, r1) == hs.get_hash(l2, r2))
            {
                ans += 2;
                if (r1 + 1 == l2)
                {
                    break;
                }
                l1 = ++r1;
                r2 = --l2;
                continue;
            }
            r1++;
            l2--;
        }
        
        if (r1 != l2 - 1)
        {
            ans++;
        }
        cout << ans endl;
    }   
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...