Cod sursa(job #2456569)

Utilizator Briana_NeaguNeagu Briana Briana_Neagu Data 14 septembrie 2019 18:12:09
Problema PScPld Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <bits/stdc++.h>
#define maxim 1000005
using namespace std;

ifstream f("pscpld.in");
ofstream g("pscpld.out");

int z[maxim * 2];

int main()
{
    string aux, s = "!";
    long long ans = 0;
    f >> aux;
    for (auto ch : aux)
    {
        s += "#";
        s += ch;
        ans ++;
    }
    s += "#";
    int n = s.length();
    int c = 0, r = 0;
    for (int i = 1; i < n; ++ i)
    {
       int op = 2 * c - i;
       if (i > r || z[op] >= r - i)
       {
           if (i > r)
               r = i;
           int k = r;
           while (k < n && s[k] == s[2 * i - k])
               k ++;
           k --;
           z[i] = k - i;
           if (k > c)
           {
               c = i;
               r = k;
           }
       }
       else
           z[i] = z[op];
       ans += z[i] / 2;
    }

    g << ans;
}