Cod sursa(job #2580977)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 14 martie 2020 13:38:28
Problema PScPld Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.37 kb
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define inf (INT_MAX/2-1)
#define infl (1LL<<60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a),end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
#define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
#define dbgs(x) cerr<<(#x)<<"[stl]: ";for(auto _:x)cerr<<_<<' ';cerr<<'\n',aaa
#define dbgp(x) cerr<<(#x)<<": "<<x.fi<<' '<<x.se<<'\n',aaa
#define maxn 1000000

using namespace std;

ifstream fin ("pscpld.in");
ofstream fout ("pscpld.out");

int n;
pii mod = make_pair(100007, 100019);
pii hp[maxn+5], hs[maxn+5];///hash prefix/sufix
pii p26[maxn+5];
string s;

pii adaug (pii h, char ch) {
  h.fi = (h.fi * 26 + ch-'a') % mod.fi;
  h.se = (h.se * 26 + ch-'a') % mod.se;
  return h;
}

pii taie (pii h, int lh, pii t, int lt) {
  h.fi = (h.fi - 1LL*p26[lh-lt].fi*t.fi%mod.fi + mod.fi) % mod.fi;
  h.se = (h.se - 1LL*p26[lh-lt].se*t.se%mod.se + mod.se) % mod.se;
  return h;
}

int main () {
  fin >> s;
  n = sz(s); s = " "+s;
  int i, j, z;
  for (p26[0] = {1,1}, i = 1; i <= n+1; i++) {
    p26[i].fi = (p26[i-1].fi * 26) % mod.fi;
    p26[i].se = (p26[i-1].se * 26) % mod.se;
  }
  for (i = 1; i <= n; i++) hp[i] = adaug(hp[i-1], s[i]);
  for (i = n; i >= 1; i--) hs[i] = adaug(hs[i+1], s[i]);
  int pas, l, r, lg;
  ll ans = 0;
  pii h1, h2;
  ///pal par
  for (i = 1; i <= n-1; i++) {
    for (lg = 0, pas = (1<<21); pas > 0; pas /= 2) {
      l = i-(lg+pas)+1;
      r = i+1+(lg+pas)-1;
      if (l >= 1 && r <= n) {
        h1 = taie(hs[l], n-l+1, hs[i+1], n-i);
        h2 = taie(hp[r], r, hp[i], i);
        if (h1 == h2) lg += pas;
      }
    }
    ans += 1LL*lg;
  }
  ///pal impar
  for (ans += 1LL*n, i = 2; i <= n-1; i++) {
    for (lg = 0, pas = (1<<21); pas > 0; pas /= 2) {
      l = i-(lg+pas);
      r = i+(lg+pas);
      if (l >= 1 && r <= n) {
        h1 = taie(hs[l], n-l+1, hs[i], n-i+1);
        h2 = taie(hp[r], r, hp[i], i);
        if (h1 == h2) lg += pas;
      }
    }
    ans += 1LL*lg;
  }
  fout << ans;
  fin.close(); fout.close();
  return 0;
}