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
#include "poszukiwania.h"
#include "message.h"

#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

#define read_pattern SignalAt
#define pattern_size SignalLength
#define read_text SeqAt
#define text_size SeqLength
#define node_id MyNodeId
#define nodes NumberOfNodes
#define push PutLL
#define pop GetLL
#define send Send
#define receive Receive

unordered_multiset<long long int> prefixes;
unordered_multiset<long long int> suffixes;
vector<int> pattern;
vector<int> text;


void find_prefixes(vector<int>& pattern, vector<int>& text) {
	for (int offset = 0; offset < (int)text.size(); ++offset) {
		bool match = true;
		for (int i = 0; i < (int)text.size() - offset; ++i) {
			if (text[i + offset] != pattern[i]) {
				match = false;
				break;
			}
		}
		if (match) {
			prefixes.insert(text.size() - offset);
		}
	}
}

void find_suffixes(vector<int>& pattern, vector<int>& text) {
	for (int offset = 0; offset < (int)text.size(); ++offset) {
		bool match = true;
		for (int i = 0; i < (int)text.size() - offset; ++i) {
			if (text[i] != pattern[i + offset]) {
				match = false;
				break;
			}
		}
		if (match) {
			suffixes.insert(text.size() - offset);
		}
	}
}

vector<long long int> receive_message() {
	vector<long long int> message;
	int node = receive(-1);
	int size = pop(node);

	if (size > 0) {
		message.reserve(size);
		for (int i = 0; i < size; ++i) {
			message.push_back(pop(node));
		}
	}
	return message;
}

template <class T>
void send_message(int target, T& message) {
	push(target, message.size());
	for (auto m: message) {
		push(target, m);
	}
	send(target);
}


int main() {
	long long int m = pattern_size();
	long long int n = text_size();
	int k = nodes();

	// for small input limit the number of used nodes
	if (k > n) {
		k = n;
		if (node_id() >= n) {
			return 0;
		}
	}

	// TODO: fix special case like 11/5
	long long int range = ceil(n / float(k));
	long long int start = node_id() * range;
	long long int end = min((node_id() + 1) * range, n);
	range = end - start;

	text.reserve(range);
	for (int i = start; i < end; ++i) {
		text.push_back(read_text(i + 1));
	}

//cout << start << " " << end << " " << range << endl;

	if (start <= n - m) {
		// read pattern beginning
		int size = min(m, range);
		pattern.reserve(size);
		for (int i = 0; i < size; ++i) {
			pattern.push_back(read_pattern(i + 1));
		}

//for (auto p: pattern) cout << p << " "; cout << "P" << endl;

		find_prefixes(pattern, text);

//for (auto p: prefixes) cout << p << " "; cout << "p" << endl;
	}

	if (end >= m) {
		// read pattern ending
		int size = min(m, range);
		pattern.clear();
		pattern.reserve(size);
		for (int i = m - size; i < m; ++i) {
			pattern.push_back(read_pattern(i + 1));
		}

//for (auto p: pattern) cout << p << " "; cout << "P" << endl;

		find_suffixes(pattern, text);

//for (auto s: suffixes) cout << s << " "; cout << "s" << endl;
	}

	if (node_id() == 0) {
		// send prefixes to next node
		push(1, prefixes.size());
		for (auto prefix: prefixes) {
			push(1, prefix);
		}
		send(1);

		// receive final results from all nodes
		int found = 0;
		for (int i = 1; i < k; ++i) {
			int node = receive(-1);
			found += pop(node);
		}

		// print results
		cout << found << endl;

	} else {
		// receive prefixes from the previous node
		auto message = receive_message();

		// check for matches
		int found = 0;
		for (auto prefix: message) {
			// pattern complete
			if (prefix == m || suffixes.count(m - prefix)) {
				++found;
			}
			// pattern continues to the next range
			if (prefix + range < m) {
//cout << "- " << prefix << endl;
				// check if the middle part of the pattern matches the text
				bool match = true;
				for (int i = 0; i < range; ++i) {
					if (text[i] != read_pattern(prefix + i + 1)) {
						match = false;
						break;
					}
				}
				if (match) {
					prefixes.insert(prefix + range);
//for (auto p: prefixes) cout << p << " "; cout << "p" << endl;
				}
			}
		}

		// send prefixes to the next node
		int target = node_id() + 1;
		if (target < k) {
			send_message(target, prefixes);
		}

		// send results to node 0
		push(0, found);
		send(0);
	}

	return 0;
}