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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "palindromy.h"
#include "message.h"
#include <bits/stdc++.h>

using namespace std;

// zrodlo: was.zaa.mimuw.edu.pl
//Palindromy parzyste
vector<int> Manacher1(string s)
{
  string x("$"); x += s; x += '#'; 
  vector<int> R;
  R.push_back(0);
  int i = 1, j = 0;
  while (i <= x.size() - 1)
  {
    while (x[i - j] == x[i + j + 1])
      j++;
    R.push_back(j);
    int k = 1;
    while (R[i - k] != R[i] - k && k <= j)
    {
      R.push_back(min(R[i - k], R[i] - k));
      k++;
    }
    j=max(j - k, 0); i += k;
  }
  return R;
}

//Palindromy nieparzyste
vector<int> Manacher2(string s)
{
  string x("$"); x += s; x += '#'; 
  vector<int> R;
  R.push_back(0);
  int i = 1, j = 0;
  while (i <= x.size() - 1)
  {
    while (x[i - j - 1] == x[i + j + 1])
      j++;
    R.push_back(j);
    int k = 1;
    while (R[i - k] != R[i] - k && k <= j)
    {
      R.push_back(min(R[i - k], R[i] - k)); 
      k++;
    }
    j=max(j - k, 0); i += k;
  }
  return R;
}

int main() 
{
	if (MyNodeId() != 0) return 0;
	
  long long N = GetLength(); 
  string s;	
  for (long long i = 0; i < N; ++ i)
  	s += GetLetter(i);
  
  vector<int> g = Manacher1(s), h = Manacher2(s);

	long long res = 0;

	for(int i = 0; i < g.size(); ++ i)
		res += g[i];
	for(int i = 0; i < h.size(); ++ i)
		res += h[i];

	cout << res +s.size() << "\n";
  
  return 0;
}