Submission #1325831

#TimeUsernameProblemLanguageResultExecution timeMemory
1325831yarkorPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
128 ms35480 KiB
#include <bits/stdc++.h>
#define db(x) cout << (#x) << " " << x<< endl
#define _USE_MATH_DEFINES
//#define db(x) ;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define coutp cout << fixed << setprecision(10)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
const ll INF = 1e18;
const int LOG = 22;
const ll base = 31;
const ll MOD = 1e9 + 7;

struct hash_t {
    ll val1;
    ull val2;

    hash_t() {
        val1 = 0, val2 = 0;
    }

    hash_t(ll a) {
        val1 = a, val2 = a;
    }

    hash_t(ll a, ull b) {
        val1 = a, val2 = b;
    }

    hash_t operator+(const hash_t &o) const{
        if (val1 + o.val1 >= MOD) {
            return {val1 + o.val1 - MOD, val2 + o.val2};
        }
        return {val1 + o.val1, val2 + o.val2};
    }

    hash_t operator-(const hash_t &o) const{
        if (val1 < o.val1) {
            return {val1 + MOD - o.val1, val2 - o.val2};
        }
        return {val1 - o.val1, val2 - o.val2};
    }

    hash_t operator*(const hash_t &o) const{
        return {val1 * o.val1 % MOD, val2 * o.val2};
    }

    bool operator==(const hash_t &o) const {
        return (val1 == o.val1 && val2 == o.val2);
    }
};

const hash_t BASE = hash_t(base);
hash_t p[ll(1e6 + 5)];

void calc() {
    p[0] = {1, 1};
    for (int i = 1; i <= 1e6 + 1; i++) {
        p[i] = p[i - 1] * BASE;
    }
}

void precalc(vector<hash_t> &h, const string &s) {
    size_t n = s.size();
    h.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        h[i] = h[i - 1] * BASE + (s[i - 1] - 'a' + 1);
    }
}

hash_t get(const vector<hash_t> &h, int i, int j) {
    return h[j] - h[i] * p[j - i];
}

void solve() {
    string s;
    cin >> s;
    vector<hash_t> h;
    precalc(h, s);
    ll l = 0, r = s.size() - 1, sz = 1;
    ll ans = 0;
    while (l < r) {
        if (get(h, l - sz + 1, l + 1) == get(h, r, r + sz)) {
            sz = 0;
            ans += 2;
        }
        l++;
        r--;
        sz++;
    }
    if (sz != 1 || l == r) {
        ans++;
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    calc();
    ll t;
    cin >> t;
    while (t--)solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...