Cod sursa(job #2426779)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 29 mai 2019 11:34:01
Problema PScPld Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'
 
using namespace std;
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
 
template<class T> bool uin(T &a, T b) {return (a < b ? false : (a = b, true));}
template<class T> bool uax(T &a, T b) {return (a > b ? false : (a = b, true));}
 
ifstream f("pscpld.in");
ofstream g("pscpld.out");
 
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen(".in", "r", stdin);
#endif
 
  string s;
  getline(f, s);
  string t(2 * s.size() + 1, '#');
  for (int i = 0; i < (int)s.size(); ++i) {
    t[2 * i + 1] = s[i];
  }
  
  vector<int> lps(t.size());
  long long ans = 0LL;
  int c = 0, r = 0;
  for (int i = 1; i < (int)t.size(); ++i) {
    int sim = 2 * c - i;
    if (i < r) {
      lps[i] = min(lps[sim], r - i);
    }
    int lft = i - lps[i];
    int rgt = i + lps[i];
    while (lft - 1 >= 0 && rgt + 1 < (int)t.size() && t[lft - 1] == t[rgt + 1]) {
      ++lps[i];
      --lft;
      ++rgt;
    }
    
    if (rgt > r) {
      r = rgt;
      c = i;
    }
    
    ans += (lps[i] + 1) / 2;
  }
  
  g << ans << endl;
 
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}