Submission #916707

#TimeUsernameProblemLanguageResultExecution timeMemory
916707vjudge1Palindromic Partitions (CEOI17_palindromic)C++17
60 / 100
7 ms1372 KiB
#include <bits/stdc++.h>

using namespace std;

int longpal_partitions(char *word)
{
    const int n=strlen(word);
    const int prime=131;

    int countedch=0;
    int piececounts=0;

    while(countedch*2<n)
    {
        int lhash=0,rhash=0,power=1;

        for(int piecesize=1;true;piecesize++)
        {
            int l=countedch,r=n-countedch-piecesize;
            if(l+piecesize-1>=r)
                return piececounts+1;
            if(piecesize==1)
                power=1;
            else
                power*=prime;
            lhash+=static_cast<int>(word[l+piecesize-1])*power;
            rhash=static_cast<int>(word[r])+rhash*prime;
            if(lhash==rhash && strncmp(word+l,word+r,piecesize)==0)
            {
                piececounts+=2;
                countedch+=piecesize;
                break;
            }
        }
    }
    return piececounts;
}

int main()
{
    int n;
    char word[510000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>word;
        assert(strlen(word)<=510000);
        cout<<longpal_partitions(word)<<endl;
    }
    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...