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
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <time.h>
#include <queue>
#include "poszukiwania.h"
#include "message.h"

typedef long long int64;

using namespace std;

void compute_lps(vector<int>& pat, vector<int>& lps) {
	int len = 0;
	int i;
	lps[0] = 0;
	i = 1;
	while (i < pat.size()) {
		if (pat[i] == pat[len]) {
			len++;
			lps[i] = len;
			i++;
		}
		else {
			if (len != 0) {
				len = lps[len-1];
			}
			else {
				lps[i] = 0;
				i++;
			}
		}
	}
}

void kmp(vector<int>& txt, vector<int>& pat, set<int>& res) {
	int M = pat.size();
	int N = txt.size();
	vector<int> lps;
	lps.reserve(M + 4);
	int j  = 0;
	compute_lps(pat, lps);
	int i = 0;
	while (i < N) {
		if (pat[j] == txt[i]) {
			j++;
			i++;
		}
		if (j == M) {
			res.insert(i - j);
			j = lps[j - 1];
		}
		else if (i < N && pat[j] != txt[i])
		{
			if (j != 0) {
				 j = lps[j - 1];
			}
			else {
				 i++;
			}
		}
	}
}

int main () {
	int N = NumberOfNodes();
	vector<int> pat1;
	pat1.push_back(1);
	vector<int> txt1;
	txt1.push_back(2);
	set<int> res1;
	kmp(pat1, txt1, res1);
	int M = MyNodeId();
	int64 pat_len = SignalLength();
	int64 txt_len = SeqLength();
	int64 pat_frags = min(pat_len, max(1LL, (int64)floor(sqrt(N))));
	int64 pat_frag_size = pat_len / pat_frags;
	if (pat_len % pat_frags) {
		pat_frag_size++;
	}
	int64 txt_frags = max(1LL, min(txt_len - pat_frag_size + 1, N / pat_frags));
	int64 nodes_used = pat_frags * txt_frags;
	if (M >= nodes_used) {
		return 0;
	}
	int64 my_pat_frag = M / txt_frags;
	int64 my_txt_frag = M % txt_frags;

	int64 my_pat_start = my_pat_frag * pat_frag_size;
	int64 my_pat_end = (my_pat_frag + 1) * pat_frag_size - 1;
	if (my_pat_frag == pat_frags - 1) {
		my_pat_end = pat_len - 1;
	}

	int64 txt_frag_size = (txt_len - pat_frag_size + 1) / txt_frags;
	if ((txt_len - pat_frag_size + 1) % txt_frags) {
		txt_frag_size++;
	}
	int64 my_txt_start = my_txt_frag * txt_frag_size;
	int64 my_txt_end = (my_txt_frag + 1) * txt_frag_size - 1;
	if (my_txt_frag == txt_frags - 1) {
		my_txt_end = txt_len - pat_frag_size;
	}
	my_txt_end += pat_frag_size - 1;

	vector<int> pat;
	pat.reserve(my_pat_end - my_pat_start + 4);
	for (int64 i = my_pat_start; i <= my_pat_end; ++i) {
		pat.push_back((int)SignalAt(i + 1));
	}
	vector<int> txt;
	txt.reserve(my_txt_end - my_txt_start + 4);
	for (int64 i = my_txt_start; i <= my_txt_end; ++i) {
		txt.push_back((int)SeqAt(i + 1));
	}
	set<int> result;
	kmp(txt, pat, result);

	if (my_pat_frag < pat_frags - 1) {
		set<int> res;
		for (int i = 0; i < txt_frags; ++i) {
			int sender = (my_pat_frag + 1) * txt_frags + i;
			Receive(sender);
			int size = GetInt(sender);
			for (int j = 0; j < size; ++j) {
				res.insert(GetInt(sender));
			}
		}
		set<int>::iterator it = result.begin();
		while (it != result.end()) {
			if (res.find(my_txt_start + *it + pat_frag_size) != res.end()) {
				it++;
			}
			else {
				it = result.erase(it);
			}
		}
	}

	if (my_pat_frag > 0) {
		vector<vector<int> > messages(txt_frags, vector<int>());
		for (set<int>::iterator it = result.begin(); it != result.end(); it++) {
			int index = my_txt_start + *it;
			int interested = index - pat_frag_size;
			if (interested >= 0) {
				int target_txt_frag = interested / txt_frag_size;
				messages[target_txt_frag].push_back(index);
			}
		}
		for (int i = 0; i < txt_frags; ++i) {
			int instance = (my_pat_frag - 1) * txt_frags + i;
			PutInt(instance, messages[i].size());
			for (int j = 0; j < messages[i].size(); ++j) {
				PutInt(instance, messages[i][j]);
			}
			Send(instance);
		}
	}

	if (M == 0) {
		int final_result = result.size();
		for (int i = 1; i < txt_frags; ++i) {
			Receive(i);
			final_result += GetInt(i);
		}
		printf("%d\n", final_result);
	}
	else if (my_pat_frag == 0) {
		PutInt(0, result.size());
		Send(0);
	}

	return 0;
}