Submission #1303380

#TimeUsernameProblemLanguageResultExecution timeMemory
1303380ndquangPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
217 ms35296 KiB
#include<bits/stdc++.h>

using namespace std;

#define endl "\n"

#define ll long long

const int base1 = 31;

const int base2 = 37;

const int Lim = 1e6+6;

const int MOD1 = 1e9+3;

const int MOD2 = 1e9+7;

int t;

int n;

string s;

ll p1[Lim];

ll p2[Lim];

ll h1[Lim];

ll h2[Lim];

int ans = 0;

void input() {

    cin >> s;

    n = (int)s.size();

    s = " " + s;

}

void build_hash() {

    p1[0] = 1;

    p2[0] = 1;

    for ( int i = 1; i <= n; i++ ) {

        p1[i] = (p1[i-1]*base1)%MOD1;

        p2[i] = (p2[i-1]*base2)%MOD2;

        h1[i] = (h1[i-1]*base1%MOD1+s[i]-'a'+1)%MOD1;

        h2[i] = (h2[i-1]*base2%MOD2+s[i]-'a'+1)%MOD2;

    }

}

pair<ll,ll> get( int l, int r ) {

    ll x = (h1[r] - h1[l-1]*p1[r-l+1]%MOD1 + MOD1)%MOD1;

    ll y = (h2[r] - h2[l-1]*p2[r-l+1]%MOD2 + MOD2)%MOD2;

    return {x,y};

}

bool check( int l, int r, int u, int v ) {

    pair<ll,ll> x = get(l,r);

    pair<ll,ll> y = get(u,v);

    return x.first == y.first && x.second == y.second;

}

void solve() {

    build_hash();

    int i = 0;

    int j = 1;

    int u = n+1;

    int v = n;

    while ( i <= n ) {

        int x = i+1;

        int y = u-1;

        if ( j == y && x == v ) {

            ans++;

            return;

        }

        if ( check(j,x,y,v) && x <= y ) {

            ans += 2;

            j = x+1;

            v = y-1;

        }

        i++;

        u--;

    }

    if ( ans == 0 ) ans++;

}

void output() {

    cout << ans << endl;

}

void reset() {

    ans = 0;

    memset(p1,0,sizeof(p1));

    memset(p2,0,sizeof(p2));

    memset(h1,0,sizeof(h1));

    memset(h2,0,sizeof(h2));

}

int32_t main() {

    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    cin >> t;

    while ( t-- ) {

        input();

        solve();

        output();

        reset();

    }

    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...