Cod sursa(job #2175150)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 16 martie 2018 15:41:18
Problema Numarare Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <iostream>
#include <fstream>
#define dimn 100005

std::ifstream f("numarare.in");
std::ofstream g("numarare.out");

int N;
int dif[2*dimn+1];
int mnch[2*dimn+1];

void manacher() {
    int center = -1, right = -1;
    for (int i=0; i<N; i++) {
        if(i<=right)                                                                            //ne aflam inauntrul unui pal
            mnch[i] = std::min(right-i+1, mnch[center - (i-center)]);

        while(i-mnch[i] >= 0 && i+mnch[i] < N && dif[i+mnch[i]] == dif[i-mnch[i]])              //expandarea
            mnch[i]++;

        if(i+mnch[i] > right)
            center = i, right = i+mnch[i];
    }

    long long ans = 0;
    for (int i=0; i<N; i++)
        ans += 1LL*mnch[i];
    g << ans;
}

void citire() {
    f >> N;
    int y, x; f >> y;
    for (int i=0; i<N; i++) {
        f >> x;
        dif[i] = y-x;
        y = x;
    } N--;
}
void rezolvare() {
    manacher();
}

int main()
{
    citire();
    rezolvare();

    return 0;
}