Cod sursa(job #2392415)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 29 martie 2019 22:56:54
Problema PScPld Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;

const string file = "pscpld";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647, nmax = 1000006;

string b;
int r[nmax*2];

int manacher(string s, int p[]) //p[i] = how many indices to move to left and right to get maximum palindrome on position i.
{
    string t;
    t.push_back(s[0]);
    for (int i = 1; i < s.length(); ++i){
        t.push_back('#');
        t.push_back(s[i]);
    }
    p[0] = 0;
    int l = 0, r = 0, c = 0, ans = 0;
    for (int i = 1; i < t.length(); ++i){
        int i_ = c*2-i;
        if(i_ <= l || i_-p[i_] <= l){
            if(i > r)
                l = i, r = i, c = i;
            else{
                c = i;
                l = c*2-r;
            }
            while(l >= 0 && r < t.length() && t[l] == t[r])
                --l, ++r;
            ++l, --r;
            p[i] = r-c;
        }else p[i] = p[i_];
    }
    for (int i = 0; i < t.length(); ++i)
        ans += p[i]/2+(t[i] != '#');
    return ans;
}

int main()
{
    ifstream fin (file+".in");
    ofstream fout (file+".out");
    fin >> b;
    fout << manacher(b, r) << "\n";
    return 0;
}