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

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {

  long long N = SeqLength();
  long long M = SignalLength();
  int nodes = NumberOfNodes();
  int id = MyNodeId();

  vector<int> seq(N),sig(M);
  for(int i=0;i<M;++i) sig[i] = SignalAt(i+1);
  for(int i=0;i<N;++i) seq[i] = SeqAt(i+1);
  //for(int i=0;i<N;++i) printf("%d",seq[i]);
  //puts("");
  //for(int i=0;i<M;++i) printf("%d",sig[i]);
  //puts("");

  int amount = (N-M)/nodes;
  int start = amount*id;
  int end = start+amount;
  if(id==nodes-1) end=N-M;
  long long result = 0;
  for(int i=start;i<end;++i){
      bool found = true;
      for(int j=0;j<M;++j){
        if(seq[i+j]!=sig[j]){
          //printf("%d+%d %d!=%d\n",i,j,seq[i+j],sig[i+j]);
          found = false;
          break;
        }
      }
      if(found) result++;
  }

  if(id==0){
    for(int i=0;i<nodes-1;++i){
      int s = Receive(-1);
      result+=GetLL(s);
    }
    printf("%lld\n",result);
  }else{
    PutLL(0,result);
    Send(0);
  }

  return 0;
}