Cod sursa(job #915966)

Utilizator danalex97Dan H Alexandru danalex97 Data 15 martie 2013 17:04:04
Problema PScPld Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <fstream>
#include <cstring>
using namespace std;

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

const int Nmax = 2000010;

int N,P[Nmax];
char A[Nmax];
int Left,Right,Ref;

void Extend(int& L,int& R,int& r)
{
    P[r] = max( (R-L+1) / 2 , int(A[r] != '#') );
    for ( ; A[--L] == A[++R] ; )
        P[r] += 2 * ( A[L] != '#' );
    L++ , R--;
}

#define Lung (Right-Poz)
#define Pair(x,Ref) ( x - Ref > 0 ? Ref - ( x - Ref ) : Ref + ( Ref - x ) )

int main()
{
    char in[Nmax>>1]; memset(in,0,sizeof(in));
    F.getline(in,Nmax>>1,'\n'); int l = strlen(in);

    for (int i=0;i<l;++i)
        A[++N] = '#', A[++N] = in[i];
    A[++N] = '#';
    A[0] = 'X';
    A[N+1] = 'Y';

    P[1] = P[2] = 0, Left = Right = Ref = 2;
    Extend(Left,Right,Ref);

    for (int Poz,Start;Ref <= N; )
    {
        Poz = Start = Ref;
        while ( Poz < Right )
        {
            ++Poz;
            if ( Lung > P[ Pair(Poz,Ref) ] )
                P[Poz] = P[ Pair(Poz,Ref) ];
            else
            {
                int Left2 = Pair(Right,Poz) , Right2 = Right;
                Extend(Left2,Right2,Poz);
                Left = Left2, Right = Right2, Ref = Poz;
                break;
            }
        }

        if ( Start == Ref )
        {
            Poz = Right+1;
            Left = Right = Ref = Poz;
            Extend(Left,Right,Ref);
        }
    }

    long long Sol = 0;
    for (int i=1;i<=N;++i)
        Sol += 1LL * (P[i]+1) / 2;
    G<<Sol<<'\n';
}