Cod sursa(job #1010464)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 14 octombrie 2013 22:40:12
Problema PScPld Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("pscpld.in");
ofstream g("pscpld.out");
char S[1000005];
int DP[1000005],Sol;
int N;
void Read()
{
    f.getline(S+1,1000005);
    N=strlen(S+1);
}
int Expand(int Left,int Right)
{
    int length=0;
    while(Left>=1 && Right<=N && S[Left]==S[Right])
    {
        length++;
        Left--;
        Right++;
    }
    return length;
}
void Solve_UnEven()
{
    int i,R=0,P=0,Left,Right;
    for(i=1;i<=N;i++)
    {
        if(R>=i)
            DP[i]=min(R-i,DP[P-(i-P)]);
        Left=i-DP[i];
        Right=i+DP[i];
        DP[i]+=Expand(Left,Right);
        if(DP[i]+i>R)
        {
            R=DP[i]+i;
            P=i;
        }
        Sol+=DP[i];
    }
}
void Solve_Even()
{
    int i,R=0,P=0,Left,Right;
    for(i=1;i<=N;i++)
    {
        if(R>=i+1)
            DP[i]=min(R-i-1,DP[P-(i-P)]);
        Left=i-DP[i];
        Right=i+DP[i]+1;
        DP[i]+=Expand(Left,Right);
        if(DP[i]+i+1>R)
        {
            R=DP[i]+i+1;
            P=i;
        }
        Sol+=DP[i];
    }
}
int main()
{
    Read();
    Solve_Even();
    Solve_UnEven();
    g<<Sol<<"\n";
    return 0;
}