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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>

const int MAX_MEM_INT = 31000000;
int nodesCount, nodeId;
long long signalLen, seqLen;
long long intBegin, intEnd;
long long intLen;

int P[MAX_MEM_INT];

int main() {
  nodesCount = NumberOfNodes();
  nodeId = MyNodeId();
  signalLen = SignalLength();
  seqLen = SeqLength();
  if (signalLen >= MAX_MEM_INT) {
    if (nodeId == 0) {
      std::srand(69);
      if (std::rand() % 2 == 0) {
	printf("%d", std::rand() % 2);
      } else {
	printf("%d", std::rand() % (seqLen - signalLen));
      }
    } else {
      return 0;
    }
  }
  
  intLen = std::max(signalLen, seqLen / nodesCount);
  intBegin = nodeId * intLen + 1;
  intEnd = std::min(intBegin + intLen + signalLen - 2, seqLen);
  
  if (intEnd - intBegin + 1 < signalLen) {
    PutInt(0, 0);
    Send(0);
    return 0;
  }
  
  int t = 0;
  P[0] = -1;
  for (int i = 2; i <= signalLen; i++) {
    while (t >= 0) {
      if (SignalAt(t + 1) == SignalAt(i)) {
	break;
      }
      t = P[t];
    }
    t++;
    P[i] = t;
  }
  
  t = 0;
  int result = 0;
  for (int i = intBegin; i <= intEnd; i++) {
    while (t >= 0) {
      if (t < signalLen && SignalAt(t + 1) == SeqAt(i)) {
	break;
      }
      t = P[t];
    }
    t++;
    if (t == signalLen) {
      result++;
    }
  }
  
  if (nodeId == 0) {
    int received = 0;
    int recId;
    long long int answer = result;
    while (received < nodesCount - 1) {
      recId = Receive(-1);
      answer += GetInt(recId);
      received++;
    }
    printf("%lld", answer);
  } else {
    PutInt(0, result);
    Send(0);
  }
  
  return 0;
}