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

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

int main() {
    long long n = SeqLength();
    long long m = SignalLength();

    vector<long long> wzor(m);
    for(long long i=0; i<m; i++)
        wzor[i] = SignalAt(i+1);

    vector<long long> psuf(m+1);
    long long p = psuf[0] = -1;
    for(long long i=1; i<=m; i++){
        while( p>-1 && wzor[p]!=wzor[i-1] ) p=psuf[p];
        psuf[i]=++p;
    }

    if(n>400000){
        long long pocz = ( MyNodeId() * (n-m) ) / NumberOfNodes();
        long long kon = ( (MyNodeId()+1) * (n-m) ) / NumberOfNodes() + m;
        long long wyn = p = 0;
        for(long long i=pocz; i<kon; i++){
            while( p>-1 && SeqAt(i+1)!=wzor[p] ) p=psuf[p];
            if( ++p==m ){
                wyn++;
                p=psuf[p];
            }
        }

        if(MyNodeId()>0){
            PutLL(0, wyn);
            Send(0);
        } else {
            for(long long i=1; i<NumberOfNodes(); i++){
                long long inst=Receive(-1);
                wyn+=GetLL(inst);
            }
            cout<<wyn;
        }
    } else if(MyNodeId()==0) {
        long long wyn = p = 0;
        for(long long i=0; i<n; i++){
            while( p>-1 && SeqAt(i+1)!=wzor[p] ) p=psuf[p];
            if( ++p==m ){
                wyn++;
                p=psuf[p];
            }
        }
        cout<<wyn;
    }

  return 0;
}