Cod sursa(job #2008934)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 8 august 2017 00:16:49
Problema PScPld Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>

using namespace std;

const int nMax = 2000005;

void Manacher(char v[], int sz, int ret[])
{
    ret[0] = 0;
    int R = -1, C = 0, rad;
    for(int i = 1; i < sz-1; ++i)
    {
        if(i <= R)
            rad = min(ret[2 * C - i], R - i);
        else
            rad = 0;
        while(i - rad >= 0 && i + rad < sz && v[i-rad] == v[i+rad])
            rad++;
        ret[i] = rad - 1;
        if(i + ret[i] > R)
        {
            C = i;
            R = i + ret[i] - 1;
        }
    }
    ret[sz-1] = 0;
}


int n;
char a[nMax];
char t[nMax];
int pal[nMax];

int main()
{
    ifstream in("pscpld.in");
    in >> t;
    in.close();
    n = strlen(t);
    for(int i = 0; i < n; ++i)
        a[2 * i] = t[i], a[2 * i + 1] = '#';
    n = 2 * n;
    Manacher(a, n, pal);
    long long rasp = 0;
    for(int i = 0; i < n; i += 2)
        rasp += (1LL * pal[i] / 2);
    for(int i = 1; i < n; i += 2)
        rasp += (1LL * pal[i] / 2 + 1);
    ofstream out("pscpld.out");
    out << rasp;
    out.close();
    return 0;
}