Home BOJ. Report Problem No.15164 (16163)
Post
Cancel

BOJ. Report Problem No.15164 (16163)

[Link] https://www.acmicpc.net/problem/16163


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <stack>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 5e5 + 1;
const int INF = 987654321;

string s;
ll ans = 0;
void solve() {
  int l_odd = 0, r_odd = -1;
  int l_even = 0, r_even = -1;
  vector<int> odd(s.size(), 1);
  vector<int> even(s.size(), 0);
  for(int i = 0; i < s.size(); i++) {
    if(i <= r_odd) odd[i] = min(r_odd - i + 1, odd[l_odd+r_odd-i]);
    if(i <= r_even) even[i] = min(r_even - i, even[l_even+r_even - i - 1]);
    while(i-odd[i]>=0 && i+odd[i]<s.size() && s[i-odd[i]] == s[i+odd[i]]) odd[i]++;
    while(i-even[i]>=0 && i+even[i]+1<s.size() && s[i-even[i]] == s[i+even[i]+1]) even[i]++;
    if(i + odd[i] - 1 > r_odd) {
      r_odd = i + odd[i] - 1;
      l_odd = i - odd[i] + 1;
    }
    if(i + even[i] > r_even) {
      r_even = i + even[i];
      l_even = i + 1 - even[i];
    }
    ans += (odd[i] + even[i]);
  }
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> s;
  solve();
  cout << ans;
  return 0;
}
This post is licensed under CC BY 4.0 by the author.