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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "poszukiwania.h"
#include "message.h"

#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

// alias rpa='/d/Programming/Projects/Potyczki-2015/rozproszone/rpa_windows_mingw/rpa.sh'
// rpa test --source=pos.cpp --nodes=5 --output=all < pos0a.in

long long N;
long long M;

long long min(long long a, long long b) {
	return a < b ? a : b;
}

void calculate(long long fromIdx, long long toIdx) {
	long long leftCount = 0;
	long long count = 0;
	long long rightCount = 0;
	long long leftIdxM = 0;
	long long rightIdxM = 0;
	long long idx = fromIdx;

	// Find left matching
	if (fromIdx > 1) {
		int matchingIdx = 0;
		long long i = 1;
		long long realToIdx = min(toIdx, fromIdx + M - 1);
		while (idx < realToIdx) {
			int matching = 1;
			for (long long j = M; j > M - i; --j) {
				if (SignalAt(j) != SeqAt(idx - (M - j))) {
					matching = 0;
					break;
				}
			}
			if (matching == 1) {
				matchingIdx = idx;
			}
			idx += 1;
			i += 1;
		}
		if (matchingIdx > 0) {
			leftCount = matchingIdx;
		}
	}

	// Proper search
	idx = fromIdx;
	long long realToIdx = (toIdx == N + 1 ? (toIdx - M + 1) : toIdx);
	while (idx < realToIdx) {
		int matching = 1;
		for (long long j = 1; j <= M; ++j) {
			if (idx + j - 1 == toIdx) {
				matching = 0;
				rightCount = idx;
				break;
			}
			if (SignalAt(j) != SeqAt(idx + j - 1)) {
				matching = 0;
				break;
			}
		}
		if (rightCount > 0) {
			break;
		}
		if (matching == 1) {
			count += 1;
		}
		idx += 1;
	}

	int receiver = 0;
	PutChar(receiver, 'F');
	PutLL(receiver, leftCount);
	PutLL(receiver, count);
	PutLL(receiver, rightCount);
	Send(receiver);
}

void findExact(long long fromIdx, long long toIdx) {
	long long count = 0;
	if (toIdx - fromIdx + 1 == M) {
		count = 1;
	} else if (toIdx - fromIdx + 1 > M) {
		long long idx = fromIdx;
		long long realToIdx = toIdx - M + 1;
		while (idx <= realToIdx) {
			int matching = 1;
			for (long long j = 1; j <= M; ++j) {
				if (SignalAt(j) != SeqAt(idx + j - 1)) {
					matching = 0;
					break;
				}
			}
			if (matching == 1) {
				count += 1;
			}
			idx += 1;
		}
	}
	int receiver = 0;
	PutChar(receiver, 'O');
	PutLL(receiver, count);
	Send(receiver);
}
const int MIN_ELEMENTS = 20;

int main() {
	int id = MyNodeId();
	int nodes = NumberOfNodes();

	N = SeqLength();
	M = SignalLength();

	long long size = N / nodes;
	if (size < MIN_ELEMENTS) {
		nodes = (N / MIN_ELEMENTS) + (N % MIN_ELEMENTS == 0 ? 0 : 1);
		size = (N / nodes) + (N % nodes == 0 ? 0 : 1);;
	}

	if (id < nodes - 1) {
		calculate(id * size + 1, id * size + size + 1);
	} else if (id == nodes - 1) {
		calculate(id * size + 1, N + 1);
	}

	if (id == 0) {
		long long result = 0;
		long long receivedFrom[nodes];
		long long leftProcessed[nodes];
		long long rightProcessed[nodes];
		
		long long leftCount[nodes];
		long long count[nodes];
		long long rightCount[nodes];
		queue<int> workers;

		int messagesIdx = nodes;
		while (messagesIdx > 0) {
			int sender = Receive(-1);
			char code = GetChar(sender);
			
			if (code == 'F') {
				receivedFrom[sender] = 1;
				leftProcessed[sender] = 0;
				rightProcessed[sender] = 0;
				leftCount[sender] = GetLL(sender);
				count[sender] = GetLL(sender);
				rightCount[sender] = GetLL(sender);

				if (sender != 0) {
					workers.push(sender);
					for (int i = 1; i < nodes; ++i) {
						if (workers.empty()) {
							break;
						}
						if (receivedFrom[i] == 1 && receivedFrom[i - 1] == 1 && leftProcessed[i] == 0 && rightProcessed[i - 1] == 0 && leftCount[i] != 0 && rightCount[i - 1] != 0) {
							leftProcessed[i] = 1;
							rightProcessed[i - 1] = 1;
							messagesIdx += 1;
							int worker = workers.front();
							workers.pop();
							PutChar(worker, 'X');
							PutLL(worker, leftCount[i]);
							PutLL(worker, rightCount[i - 1]);
							Send(worker); 
						}
					}
				}
			} else if (code == 'O') {
				result += GetLL(sender);
			}
			messagesIdx -= 1;
		}

		while (!workers.empty()) {
			int worker = workers.front();
			workers.pop();
			PutChar(worker, 'Q');
			Send(worker);
		}

		long long longJohns[nodes];
		int johnIdx = 0;
		for (int i = 0; i < nodes; ++i) {
			if (leftCount[i] != 0 && leftProcessed[i] == 0) {
				longJohns[johnIdx++] = leftCount[i];
			}
		}
		if (johnIdx > 0) {
			for (int i = 1; i < nodes; ++i) {
				if (rightCount[i] != 0 && rightProcessed[i] == 0) {
					long long rightJohn = rightCount[i];
					for (int j = 0; j < johnIdx; j++) {
						long long leftJohn = longJohns[j];
						if (leftJohn - rightJohn + 1 == M) {
							//cout << "W morde jeza! " <<endl;
							result += 1;
						}
					}
				}
			}
		}

		for (int i = 0; i < nodes; i++) {
			result += count[i];
		}

		/*for (int i = 0; i < nodes; ++i) {
			cout << "Worker: " << i << " Left: " <<leftCount[i]<<" Count: "<<count[i]<<" Right: "<<rightCount[i]<<" From: " << i * size + 1 << " To: " << i * size + size + 1<<endl;
		}*/
		cout << result << endl;
	} else if (id <= nodes - 1) {
		Receive(0);
		char code = GetChar(0);
		if (code == 'X') {
			findExact(GetLL(0), GetLL(0));
		}
	}
	return 0;
}