Submission #1303065

#TimeUsernameProblemLanguageResultExecution timeMemory
1303065bonr_to_debugPalindromic Partitions (CEOI17_palindromic)C++20
100 / 100
113 ms11040 KiB
#include <bits/stdc++.h>
using namespace std;
const int base = 256;
const int mod = 1000000007 + 2277;
int pw[1000005], hs[1000005];
int geth(int l, int r){
    return (hs[r] - 1LL * hs[l-1] * pw[r-l+1] % mod + mod) % mod;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin>>t;
    while (t--)
    {
        string s;
        cin >> s;
        int n = s.size();
        pw[0] = 1;
        for(int i = 1; i <= n; i++){
            pw[i] = 1LL * pw[i-1] * base % mod;
        }
        s = ' ' + s;
        hs[0]=0;
        for(int i = 1; i <= n; i++)
        {
            hs[i] = (1LL * hs[i-1] * base + s[i]) % mod;
        }
        int i=1,j=n;
        int res=0;
        while (i<=j)
        {
            for (int len=0;len+i<=j;len++)
            {
                if (geth(i,i+len)==geth(j-len,j)){
                    if (i+len==j) res++;
                    else res+=2;
                    i=i+len+1;
                    j=j-len-1;
                    break;
                }
            }
        }
        cout<<res<<'\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...