Cod sursa(job #1299821)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 23 decembrie 2014 21:22:58
Problema PScPld Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

const int nmax = 1000005;

int i, n, p[2 * nmax];
ll sol;
char s[nmax], t[2 * nmax];

int main()
{
    cin.sync_with_stdio(false);

    freopen("pscpld.in", "r", stdin);
    freopen("pscpld.out", "w", stdout);

    cin >> s;
    n = strlen(s);

    int N = 0;
    t[0] = '$';
    for(int i = 0; i < n; i++)
    {
        t[++N] = s[i];
        p[N] = 1;
        t[++N] = '$';
    }

    int C = 0;
    int R = 0;
    for(int i = 1; i < N; i++)
    {
        int mirror = 2 * C - i;

        if(i <= R)
            p[i] = min(p[mirror], R - i + 1);

        int l = i - p[i] + 1;
        int r = i + p[i] - 1;

        while(l - 2 >= 1 && r + 2 <= N && t[l - 2] == t[r + 2])
        {
            l -= 2;
            r += 2;
            p[i] += 2;
        }

        if(r > R)
        {
            R = r;
            C = i;
        }

        sol += (p[i] + 1) / 2;
    }

    cout << sol << '\n';

    return 0;
}