Submission #916011

#TimeUsernameProblemLanguageResultExecution timeMemory
916011vjudge1Palindromic Partitions (CEOI17_palindromic)C++17
100 / 100
169 ms20076 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
int main()
{
    int t, p = 47;
    cin>>t;
    vector<ll> pw(1000001);
    pw[0] = 1;
    for (int i = 1; i <= 1000001; i++){
        pw[i] = (pw[i - 1] * p) % mod;
    }
    for (int it = 0; it < t; it++){
        string s;
        cin>>s;
        int l = 0, lind = -1, r = s.size() - 1;
        ll lhash = 0, rhash = 0, c = 0;
        while (l < r){
            lind++;
            lhash += s[l] * pw[lind];
            lhash %= mod;
            rhash = rhash * p + s[r];
            rhash %= mod;
            if (lhash == rhash){
                c += 2;
                lhash = 0;
                rhash = 0;
                lind = -1;
            }
            l++;
            r--;
        }
        if (l == r || lhash != 0 || rhash != 0){
            c++;
        }
        cout<<c<<"\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...