Submission #1114907

#TimeUsernameProblemLanguageResultExecution timeMemory
1114907vladiliusPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
328 ms27812 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int base = 31;

int f(char x){
    return x - 'a' + 1;
}

struct hash_str{
    vector<ll> pm, hm;
    void init(string s){
        int n = (int) s.length();
        pm.resize(n + 1);
        pm[0] = 1;
        for (int i = 1; i <= n; i++){
            pm[i] = (pm[i - 1] * base) % mod;
        }
        hm.resize(n + 1);
        hm[0] = 0;
        for (int i = 1; i <= n; i++){
            hm[i] = (hm[i - 1] * base + f(s[i - 1])) % mod;
        }
    }
    ll get(int l, int r){
        return ((hm[r] - hm[l - 1] * pm[r - l + 1]) % mod + mod) % mod;
    }
};

int main(){
    int t; cin>>t;
    while (t--){
        string s; cin>>s;
        hash_str sol; sol.init(s);
        int i = 1, j = 1, n = (int) s.length(), cnt = 0;;
        while (2 * j <= n){
            if (sol.get(i, j) == sol.get(n - j + 1, n - i + 1)){
                i = j + 1;
                cnt++;
            }
            j++;
        }
        cout<<2 * cnt + (i <= n - i + 1)<<"\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...