Cod sursa(job #263724)

Utilizator Mishu91Andrei Misarca Mishu91 Data 20 februarie 2009 19:31:35
Problema PScPld Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <cstdio>

#define MAX_N 2000004

char S[MAX_N];
int N, bst = 1, L[MAX_N];
long long Sol;

inline int Min(const int a, const int b)
{
    return ((a) < (b)) ? (a) : (b);
}

void citire()
{
    for(N = 2; scanf("%c",S+N-1) != EOF; N += 2);
    N -= 2;
    if(S[N - 1] == '\n')
        N -= 2;
}

void solve()
{
    for(int i = 1; i < N; ++i)
    {
        if(bst + L[bst] - 1 < i)
            L[i] = (i & 1);
        else
            L[i] = Min(L[2 * bst - i], bst + L[bst] - i);
            
        int j = L[i] + 1;

        while(i - j > 0 && i + j <= N && S[i + j] == S[i - j])
            j += 2;

        L[i] = j - 1;

        if(i + L[i] > bst + L[bst])
            bst = i;
        Sol += (L[i] + 1) >> 1;
    }
    printf("%lld\n",Sol);
}

int main()
{
    freopen("pscpld.in","rt",stdin);
    freopen("pscpld.out","wt",stdout);

    citire();
    solve();
}