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
#include "palindromy.h"
#include "message.h"
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N;

inline char s(int i) {
	if(i == 0) return '$';
	else if(i == N + 1) return '#';
	else return GetLetter(i - 1);
}

int main() {
	if(MyNodeId() > 1) return 0;
	N = GetLength();
	vector <int> r(N + 1, 0);
	int i = 1;
	int t = 0;
	long long wynik = 0;
	
	if(MyNodeId() == 0) {
		while(i <= N) {
			while(s(i - t) == s(i + t + 1)) t++;
			r[i] = t;
			wynik += t;
			int k = 1;
			while(k <= t && r[i - k] != r[i] - k) {
				r[i + k] = min(r[i - k], r[i] - k);
				wynik += r[i + k];
				k++;
			}
			i += k;
			t = max(0, t - k);
		}	
	
		Receive(1);
		wynik += GetLL(1);
		cout << wynik << endl;
	}
	else {
		while(i <= N) {
			while(s(i - t - 1) == s(i + t + 1)) t++;
			r[i] = t;
			wynik += t;
			int k = 1;	
			while(k <= t && r[i - k] != r[i] - k) {
				r[i + k] = min(r[i - k], r[i] - k);
				wynik += r[i + k];
				k++;
			}
			i += k;
			t = max(0, t - k);			
		}
		wynik += N;
		PutLL(0, wynik);
		Send(0);
	}
	
	return 0;
}