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

using namespace std;
/*
 * MESSAGE.H:
 * int NumberOfNodes();
 * int myNodeId();
 * 
 * 
 * [T] = Char | Int | LL
 * 
 * Put[T](int target, [T] value)
 * send(int target)
 * 
 * int receive(int target)
 * [T] Get[T](int source)
 * */
 
pair<int, int> getStartEnd(int n, int m, int nodeID, int totalNodes){
	int startPoints = n - m + 1;
	int SPPerInstance = ceil((double)startPoints / (double)totalNodes);
	int start = SPPerInstance * nodeID;
	int end = start + SPPerInstance - 1;
	if(end >= startPoints){
		end = startPoints - 1;
	}
	return pair<int, int>(start, end);
}

using namespace std;
int main(){
	int NodesN = NumberOfNodes();
	int myNode = MyNodeId();
	
	long long tLen = SeqLength();
	long long pLen = SignalLength();
	
	pair<int, int> SE = getStartEnd(tLen, pLen, myNode, NodesN);
	int start = SE.first + 1;
	int end = SE.second + 1;
	
	int result = 0;
	if(start <= end){
		int *pref = new int[pLen + 1];
		int *wzor = new int[pLen + 1];
		pref[1] = 0;
		int k = 0;
		// SZUKAMY WYSTĄPIEŃ WZORCA W TEKŚCIE
		wzor[1] = SignalAt(1);
		for(int i = 2; i <= pLen; ++i){
			wzor[i] = SignalAt(i);
			while(k > 0 && wzor[k + 1] != wzor[i]){
				k = pref[k];
			}
			if(wzor[k + 1] == wzor[i]){
				k = k + 1;
			}
			pref[i] = k;
		}
		
		// tablica pref[q] ma wszystko, co potrzeba
		int textend = end + pLen - 1;
		int q = 0;
		for(int i = start; i <= textend; ++i){
			int txti = SeqAt(i);
			while(q > 0 && wzor[q + 1] != txti){
				q = pref[q];
			}
			if(wzor[q + 1] == txti){
				q = q + 1;
			}
			if(q == pLen){
				result = result + 1;
				q = pref[q];
			}
		}		
	}	
	
	if(myNode == 0){
		// odbieramy i wypisujemy sumę
		for(int i = 1; i < NodesN; ++i){
			int src = Receive(-1);
			result += GetInt(src);
		}
	
		cout << result << endl;
	}else{
		// wysyłamy sumę punktów spełniających wymagania
		PutInt(0, result);
		Send(0);
	}
	
	return 0;
}