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
#include <bits/stdc++.h>
#include "poszukiwania.h"
#include "message.h"

using namespace std;

int n, m;
int id, nodes, matching_start, res, part_res, limit;

inline int get_matching_start(int id);

int main() {
    n = SignalLength();
    m = SeqLength();
    id = MyNodeId();
    nodes = NumberOfNodes();

    matching_start = get_matching_start(id);
    //printf("matching start: %d for %d\n", matching_start, id);
    limit = min(m - n, get_matching_start(id + 1));
    if(id == 0 || matching_start != get_matching_start(id - 1)) {
        //printf("matching start: %d for %d\n", matching_start, id);
        if(limit == 0) limit = m - n;
        for(int i = matching_start; i < limit; ++i) {
            //printf("comparing\n");
            bool match = true;
            for(int j = 0; j < n; ++j) {
                //printf("%d:%d\n", j, i+j);
                //printf("comparing %d with %d\n", SignalAt(j + 1), SeqAt(i+j + 1));
                if(SignalAt(j + 1) != SeqAt(i + j + 1)) {
                    match = false;
                    break;
                }
            }
            if(match) ++part_res;
        } 
    }
    
    if(id == 0) {
        res = part_res;
        for(int i = 1; i < nodes; ++i) {
            Receive(i);
            int msg = GetInt(i);
            //printf("received %d from %d\n", msg, i);
            res += msg;
        }
        printf("%d\n", res);
    } else {
        PutInt(0, part_res);
        Send(0);
    }
    
    return 0;
}

inline int get_matching_start(int id) {
    return id * ((m - n) / nodes);
}