Cod sursa(job #956673)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 3 iunie 2013 16:50:29
Problema PScPld Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

ifstream in ("pscpld.in");
ofstream out ("pscpld.out");

const int MAXN = 1000010;

char S[MAXN];
int Best[MAXN * 2], X[MAXN * 2];

int main()
{
    int N, i, center = -1, right = -1, st, dr, r;
    long long Ans = 0;

    in >> (S + 1);
    N = strlen (S + 1);
    for (i = 1; i <= N; i ++)
        X[2 * i] = S[i] - 'a' + 1;
    N = 2 * N + 1;
    X[0] = -1, X[N + 1] = -2;

    for (i = 1; i <= N; i ++){
        st = dr = i;

        if (i > right)
            Best[i] = 0;
        else{
            r = i - center;
            Best[i] = Best[center - r];
            if (i + Best[i] > right)
                Best[i] = right - i;
            st -= Best[i];
            dr += Best[i];
        }

        while (X[st - 1] == X[dr + 1]){
            ++ Best[i];
            -- st, ++ dr;
        }

        if (dr > right)
            right = dr, center = i;

        Ans += (Best[i] + 1) / 2;
    }

    out << Ans;

    return 0;
}