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
#include<set>
#include<cstdio>


#include "message.h"
#include "poszukiwania.h"

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

struct compare_first {
  bool operator ()(pair<int, int> const& a, pair<int, int> const& b) {
    return a.first < b.first;
  }
};

int procs, pid, sig_len, seq_len, _begin, _end;

int * ps;

set<pair<int, int>, compare_first> cache;

int get_ps(int at) {
  if (_begin <= at && at < _end) {
    return ps[at - _begin];
  }
  auto it = cache.find(make_pair(at, -1));
  if (it != cache.end()) {
    return it->second;
  }
  int guess_pid = (at * (long long)procs) / sig_len;
  while ( (guess_pid * (long long)sig_len) / procs > at) {
    --guess_pid ;
  }
  while ( ((guess_pid + 1) * (long long)sig_len) / procs <= at) {
    ++guess_pid ;
  }
  PutInt(guess_pid, at);
  //printf("sending %d from %d to %d\n", at, pid, guess_pid);
  Send(guess_pid);
  Receive(guess_pid);
  //printf("receiving somiethin from %d at %d\n", guess_pid, pid);
  int result = GetInt(guess_pid);
  cache.insert(make_pair(at, result));
  return result;
}

void start_replying_job() {
  while (true) {
    int id = Receive(-1);
    int at = GetInt(id);
    //printf("received %d from %d at %d\n", at, id, pid);
    if (at == -1) {
      break;
    }
    int value = ps[at - _begin];
    PutInt(id, value);
    //printf("sending %d from %d to %d\n", value, pid, id);
    Send(id);
  }
}

void compute_ps_function() {
  ps = new int[_end - _begin];
  
  int matched = 0;
  if (pid > 0) {
    int x = Receive(pid - 1);
    matched = GetInt(pid - 1);
  }
  int _index = _begin;
  while (_index < _end) {
    while ((matched > 0) && (matched == sig_len || SignalAt(matched + 1) != SignalAt(_index + 1))) {
      matched = get_ps(matched - 1);
    }
    if (SignalAt(matched + 1) == SignalAt(_index + 1) && matched != _index) {
      matched++;
    }
    ps[_index - _begin] = matched;
    _index++;
  }
  if (pid < procs - 1) {
    PutInt(pid + 1, matched);
    //printf("sending %d from %d to %d\n", matched, pid, pid + 1);
    Send(pid + 1);
    start_replying_job();
  } else {
    REP(i, procs - 1) {
      PutInt(i, -1);
    //printf("sending -1 from %d to %d\n", pid, i);
      Send(i);
    }
  }
}

int main() {
  procs = NumberOfNodes();
  pid = MyNodeId();
  sig_len = SignalLength();
  seq_len = SeqLength();
  
  _begin = (pid * (long long)sig_len) / procs;
  _end = ((pid + 1) * (long long)sig_len) / procs;
  //  printf("%d A\n", pid);
  compute_ps_function();
  //printf("%d B\n", pid);
  
  if (pid == 0) {
  //printf("%d C\n", pid);
    int p_cnt = 0, matched = 0;
    FOR(i, 0, seq_len) {
      while (matched > 0 &&
          ((matched == sig_len) || (SignalAt(matched + 1) != SeqAt(i + 1)))) {
        matched = get_ps(matched - 1);
      }
      if (SignalAt(matched + 1) == SeqAt(i + 1)) {
        matched++;
      }
      if (matched == sig_len) {
        ++p_cnt;
      }
      //printf("pid %d at: %d matched: %d\n", pid, i, matched);
    }
  //printf("%d D\n", pid);
    printf("%d\n", p_cnt);
    FOR(i, 1, procs) {
      PutInt(i, -1);
      Send(i);
    }
  } else {
  //printf("%d E\n", pid);
    start_replying_job();
  //printf("%d F\n", pid);
  }
  
  delete [] ps;
  
  return 0; 
}