Cod sursa(job #2578912)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 11 martie 2020 18:36:32
Problema PScPld Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second
#define Nmax 500005
const long double PI = acos(-1);


struct manacher{
  string s;
  vector<int> v;
  // the total number of palindroms
  long long sum;

  manacher(string _s){
    for (auto it : _s){
      s += '&';
      s += it;
    }
    s += '&';
    solve();
  }

  void solve(){
    int lg = 0, dr = 0, c = 0;
    v.resize(s.size());
    for (int i=0;i<s.size();i++){
      if(i <= dr)
        lg = min(v[2 * c - i], dr - i + 1);

      while (i - lg - 1 >= 0 && i + lg + 1 < s.size() && s[i - lg - 1] == s[i + lg + 1]) lg++;
      v[i] = lg;
      if (i + lg > dr){
        dr = i + lg;
        c = i;
      }
      if (i % 2) sum += lg / 2 + 1;
      else sum += (lg + 1) / 2; 
    }
    // dbg(s);
    // dbg(v);
  }

  int getMyPos(int x){
    return x * 2 - 1;
  }

  int getVal(int x){
    x = getMyPos(x);
    return v[x] / 2 + 1;
  }

  // palindroms of par length
  int getValPar(int x){
    x = getMyPos(x) + 1;
    return (v[x] + 1) / 2;
  }
};

string s;
int main(){
  ios::sync_with_stdio(false);
  freopen("pscpld.in","r",stdin);
  freopen("pscpld.out","w",stdout);
  cin >> s;
  manacher * man = new manacher(s);
  cout << man->sum << '\n';
  return 0;
}