Cod sursa(job #3214881)

Utilizator not_anduAndu Scheusan not_andu Data 14 martie 2024 15:25:31
Problema PScPld Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#pragma GCC optimize ("03", "Ofast", "unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define INFILE "pscpld.in"
#define OUTFILE "pscpld.out"

typedef long long ll;

vector<int> manacher(string s) {

    int n = s.length();
    vector<int> z(n);
    int left = 0, right = 0;
    for(int i = 0; i < n; ++i){
        if(i < right) z[i] = min(right - i + 1, z[left + right - i]);
        while(i - z[i] >= 0 && i + z[i] < n && s[i - z[i]] == s[i + z[i]]) ++z[i];
        if(i + z[i] > right) left = i - z[i] + 1, right = i + z[i] - 1;
    }

    return z;

}

void solve(){

    string aux; cin >> aux;
    string s;

    for(int i = 0; i < aux.length(); ++i){
        s += '#';
        s += aux[i];
    }

    s += '#';

    ll ans = 0;
    vector<int> z = manacher(s);
    for(int i = 0; i < z.size(); ++i){
        ans += z[i] / 2;
    }

    cout << ans << '\n';

}

int main(){
    ios_base::sync_with_stdio(false);
    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);
    cin.tie(0), cout.tie(0);
    solve();
    return 0;
}