Submission #561165

#TimeUsernameProblemLanguageResultExecution timeMemory
561165fatemetmhrPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
310 ms28456 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb      push_back
#define all(x)  x.begin(), x.end()
#define fi      first
#define se      second

const int maxn5 = 1e6 + 10;

ll base[2] = {31, 37};
ll mod[2]  = {1000000009, 1000000007};

int hass[2][maxn5], pw[2][maxn5];

inline bool is_eq(int l1, int r1, int l2, int r2){
    for(int j = 0; j < 2; j++){
        ll tmp1, tmp2;
        tmp1 = (mod[j] + hass[j][r1] - (l1 > 0 ? ll(hass[j][l1 - 1]) * pw[j][r1 - l1 + 1] % mod[j] : 0)) % mod[j];
        tmp2 = (mod[j] + hass[j][r2] - (l2 > 0 ? ll(hass[j][l2 - 1]) * pw[j][r2 - l2 + 1] % mod[j] : 0)) % mod[j];
        if(tmp1 != tmp2)
            return false;
    }
    return true;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    for(int j = 0; j < 2; j++){
        pw[j][0] = 1;
        for(int i = 1; i < maxn5; i++)
            pw[j][i] = pw[j][i - 1] * base[j] % mod[j];
    }

    int tt; cin >> tt;
    while(tt--){
        string s; cin >> s;
        int n = s.size();
        ll last[2] = {0, 0};
        for(int j = 0; j < 2; j++) for(int i = 0; i < n; i++){
            last[j] = (last[j] * base[j] + s[i] - 'a' + 1) % mod[j];
            hass[j][i] = last[j];
        }
        int ans = 0;
        int fr = 0;
        for(int i = 0; i < n; i++){
            if((i - fr + 1) * 2 > n - fr){
                ans++;
                break;
            }
            if(is_eq(fr, i, n - (i - fr + 1), n - 1)){
                ans += 2;
                n -= i - fr + 1;
                fr = i + 1;
            }
        }
        cout << ans << '\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...