Cod sursa(job #2909272)

Utilizator mihneazzzMacovei Daniel mihneazzz Data 11 iunie 2022 00:24:52
Problema PScPld Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>
#define N 1000005
using namespace std;
ifstream fin("pscpld.in");
ofstream fout("pscpld.out");
int n,d[2*N],ct;
string s;
void ManacherImpar(string s)
{
    n=s.size();
    s="$"+s+"^";
    int l=1,r=1;
    for(int i=1;i<=n;i++)
    {
        d[i]=max(0,min(r-i,d[l+(r-i)]));
        while(s[i-d[i]]==s[i+d[i]]) d[i]++;
        if(i+d[i]>r) l=i-d[i],r=i+d[i];
    }
}
void Manacher(string s)
{
    string t;
    for(auto c:s) t+=string("#")+c;
    ManacherImpar(t+"#");
}
int main()
{
    fin>>s;
    Manacher(s);
    for(int i=1;i<=n;i+=2) ct+=(d[i]-1)/2;
    for(int i=2;i<=n;i+=2) ct+=d[i]/2;
    fout<<ct;
    return 0;
}