Submission #1086423

#TimeUsernameProblemLanguageResultExecution timeMemory
1086423serifefedartarPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
749 ms20612 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
#define f first
#define s second
#define LOGN 21
const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 100;

const ll P = 31;
const ll MD = 1e9 + 9;

ll expo(ll A, ll B) {
    ll res = 1;
    while (B) {
        if (B & 1)
            res = (res * A) % MD;
        A = (A * A) % MD;
        B /= 2;
    }
    return res;
}

ll hh[MAXN];
ll f(int l, int r) {
    return (hh[r] - hh[l-1] * expo(P, r - l + 1) % MD + MD) % MD;
}

void solve() {
    string S;
    cin >> S;

    int N = S.length();
    S = "#" + S;
    for (int i = 1; i <= N; i++)
        hh[i] = (hh[i-1] * P + (S[i] - 'a' + 1)) % MD;

    bool check = true;
    int cnt = 0;
    for (int l = 1, r = 1; l <= N; ) {
        while (f(l, r) != f(N - r + 1, N - l + 1) && r <= N)
            r++;

        cnt++;
        if (r > N)
            check = false;
        l = r + 1;
        r = r + 1;
    }

    if (check)
        cout << cnt << "\n";
    else
        cout << "-1\n";
}

int main() {
    fast;
    int 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...