Cod sursa(job #261324)

Utilizator MariusMarius Stroe Marius Data 18 februarie 2009 02:42:23
Problema PScPld Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <iostream>
#include <fstream>
using namespace std;

const char iname[] = "pscpld.in";
const char oname[] = "pscpld.out";

const int MAXN = 2000005;

#define Min(a, b) ((a) < (b) ? (a) : (b))

char S[MAXN];  int Lg[MAXN];

void work(const int cnt) {
    srand((unsigned)time(NULL));
    ofstream out(iname);

    for (int i = 0; i < cnt; ++ i)
        out << (char)(rand() % 1 + 'a');
    out.close();
}

int main(void) {
//  work(1000000);

    ifstream in(iname);
    int n = 0;

    while (in >> S[2 * n + 1])
        n ++;
    in.close();

    ofstream out(oname);
    long long res = 0;
    int idx = 1, lg = 1;
    for (int i = 1; i < 2 * n; ++ i)
    {
        if (idx + Lg[idx] - 1 < i)
            Lg[i] = (i % 2) ? 1 : 0;
        else
            Lg[i] = Min(Lg[idx - (i - idx)], idx + Lg[idx] - i);

        int j = Lg[i] + 1;

        while (i - j >= 1 && i + j < 2 * n && S[i - j] == S[i + j])
            j += 2;
        j -= 2;
        Lg[i] = j + 1;

        if (i + Lg[i] > idx + Lg[idx])
            idx = i;

        res += (Lg[i] + 1) / 2;
    }
    out << res;

    in.close(), out.close();
    return 0;
}