Cod sursa(job #1256147)

Utilizator xtreme77Patrick Sava xtreme77 Data 5 noiembrie 2014 20:29:03
Problema PScPld Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
/*
 * Code by Spiromanul
 */

#include <fstream>
#include <cstring>

const char IN [ ] = "pscpld.in" ;
const char OUT [ ] = "pscpld.out" ;
const int MAX = 1000014 ;

using namespace std;

ifstream fin ( IN ) ;
ofstream fout ( OUT ) ;

char sir [ MAX ] ;
char psc [ MAX + MAX ] ;

int DP [ MAX + MAX ] ;

int main(              )
{
    fin >> sir ;
    int l = strlen ( sir ) ;
    int n = 2 ;
    psc [ 0 ] = '@' ;
    psc [ 1 ] = '$' ;
    for ( int i = 0 ; i < l ; ++ i )
    {
        psc [ n ++ ] = sir [ i ] ;
        psc [ n ++ ] = '$' ;
    }
    psc [ n ] = '\0' ;
    DP [ 1 ] = DP [ 0 ] = 1 ;
    int mit = 1 ;
    int id = 1 ;
    for ( int i = 2 ; i < n ; ++ i )
    {
        if ( mit > i )
            DP [ i ] = min ( DP [ i * 2 - id ] , mit - i ) ;
        else DP [ i ] = 1 ;
        while ( psc [ i + DP [ i ] ] == psc [ i - DP [ i ] ]  )
            DP [ i ] ++ ;
        if ( i + DP [ i ] > mit )
        {
            mit = i + DP [ i ] ;
            id = i ;
        }
    }
    long long sol = 0 ;
    for ( int i = 1 ; i < n ; ++ i )
        sol += DP [ i ] >> 1 ;
    fout << sol << '\n' ;
    return 0;
}