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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "poszukiwania.h"
#include "message.h"

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>

using namespace std;

vector<int> P, T;
int S, M;
map<int, int> Cache;

inline int getInstanceForIndex(int index, int blockSize) {
	return (index - 1) / blockSize;
}

int getP(int index, int p, int k, int blockSize) {
	if (index == 0) return -1;

	int inst = getInstanceForIndex(index, blockSize);
	if (inst == k) return P[index - k * blockSize - 1];

	auto it = Cache.find(index);
	if (it != Cache.end()) {
		return it->second;
	}
	else {
		PutChar(inst, 'p');
		PutInt(inst, index);
		Send(inst);

		Receive(inst);
		return (Cache[index] = GetInt(inst));
	}
}

int getT(int index, int p, int k, int blockSize) {
	int inst = getInstanceForIndex(index, blockSize);
	if (inst == k) return T[index - k * blockSize - 1];

	int x;
	if (index <= S) {
		x = SignalAt(index);
	}
	else if (index == S + 1) {
		x = -1;
	}
	else {
		x = SeqAt(index - S - 1);
	}

	return x;
}

int main(int argc, char *argv[]) {
	int res = 0, partialRes, t, x;
	int k = MyNodeId();
	int p = NumberOfNodes();
	S = SignalLength();
	M = SeqLength();

	int inputLength = 1 + S + M;
	int blockSize = (inputLength / p) + (int)(inputLength % p != 0);
	int left = k * blockSize + 1;
	int right = min(inputLength, left + blockSize - 1);

	if (left <= inputLength) {
		int myBlockSize = right - left + 1;
		P.resize(myBlockSize);
		T.resize(myBlockSize);

		t = -1;
		for (int i = left; i <= right; ++i) {
			if (i <= S) {
				x = SignalAt(i);
			}
			else if (i == S + 1) {
				x = -1;
			}
			else {
				x = SeqAt(i - S - 1);
			}
			T[i - left] = x;
		}

		if (k > 0) {
			Receive(k - 1);
			t = GetInt(k - 1);
		}

		for (int i = left; i <= right; ++i) {
			while (t >= 0 && getT(t + 1, p, k, blockSize) != T[i - left]) t = getP(t, p, k, blockSize);
			P[i - left] = ++t;
		}

		bool wait = true;
		if (k < p - 1 && right < inputLength) {
			PutInt(k + 1, t);
			Send(k + 1);
		}
		else if (right == inputLength) { // last process - end of work
			wait = false;
			for (int i = 0; i < p; ++i)
				if (i != k) {
					PutChar(i, 'e');
					Send(i);
				}
		}

		while (wait) {
			int from = Receive(-1);
			char c = GetChar(from);
			if (c == 'e') {
				wait = false;
			}
			else {
				int x = GetInt(from);
				x -= left;
				if (c == 't') PutInt(from, T[x]);
				if (c == 'p') PutInt(from, P[x]);
				Send(from);
			}
		}

		for (int i = 0; i < P.size(); ++i)
			if (P[i] == S) ++res;
	}
	else {
		int from = Receive(-1);
		char c = GetChar(from);
	}

	if (k == 0) {
		for (int i = 1; i < p; ++i) {
			Receive(i);
			partialRes = GetInt(i);
			res += partialRes;
		}
		cout << res << endl;
	}
	else {
		PutInt(0, res);
		Send(0);
	}

	return 0;
}