제출 #550543

#제출 시각아이디문제언어결과실행 시간메모리
550543JomnoiPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
66 ms26672 KiB
#include <bits/stdc++.h>
#define DEBUG false
using namespace std;

const int MAX_N = 1e6 + 10;
const long long P = 9973;
const long long MOD1 = 1e9 + 9;
const long long MOD2 = 1e9 + 7;

char s[MAX_N];
long long p1[MAX_N], p2[MAX_N];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    p1[0] = p2[0] = 1;
    for(int i = 1; i < MAX_N; i++) {
        p1[i] = p1[i - 1] * P % MOD1;
        p2[i] = p2[i - 1] * P % MOD2;
    }
    while(t--) {
        cin >> (s + 1);
        int n = strlen(s + 1);
        for(int i = 1; i <= n; i++) {
            s[i] = s[i] - 'a' + 1;
        }

        int ans = 0;
        long long h1 = 0, h2 = 0, rh1 = 0, rh2 = 0;
        int i, j, l = 0, r = n + 1;
        for(i = 1, j = n; i < j; i++, j--) {
            h1 = (h1 * P + s[i]) % MOD1;
            h2 = (h2 * P + s[i]) % MOD2;
            rh1 = (rh1 + p1[r - j - 1] * s[j]) % MOD1;
            rh2 = (rh2 + p2[r - j - 1] * s[j]) % MOD2;

            if(h1 == rh1 and h2 == rh2) {
                ans += 2;
                l = i, r = j;
                h1 = h2 = rh1 = rh2 = 0;
            }
        }
        if((i - 1 != l and j + 1 != r) or n % 2 == 1) {
            ans++;
        }
        cout << ans << '\n';
    }
    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...