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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "poszukiwania.h"
#include "message.h"

#include <iostream>
#include <vector>
#include <limits>


long long search(long long s_len, long long m_len, long long start, long long end)
{
    long long res = 0;

    long long sum_s = 0;
    long long sum = 0;

    for (long long i = 0; i < s_len; ++i)
    {
        sum_s += SignalAt(i + 1);
        sum += SeqAt(start + i);
    }

    for (long long i = start; i < end; ++i)
    {
        if (sum_s == sum)
        {
            bool found = true;

            for (long long j = 1; j < s_len + 1; ++j)
            {
                if (SignalAt(j) != SeqAt(i + j - 1)) {
                    found = false;
                    break;
                }
            }

            if (found) ++res;
        }

        if (i + s_len <= m_len)
        {
            sum -= SeqAt(i);
            sum += SeqAt(i + s_len);
        }
    }

    return res;
}


long long searchKMP(long long s_len, long long m_len, long long start, long long end)
{
    long long res = 0;

    //
    long long i = 0, j = -1;
    std::vector<long long> next(s_len + 1, -1);

    while (i < s_len)
    {
        while (j >= 0 && SignalAt(i + 1) != SignalAt(j + 1))
            j = next[j];

        i = i + 1;
        j = j + 1;

        if (i < s_len && SignalAt(i + 1) == SignalAt(j + 1))
            next[i] = next[j];
        else
            next[i] = j;
    }

    //
    i = start - 1;
    j = 0;

    while (i < end + s_len - 1)
    {
        while (j >= 0 && SignalAt(j + 1) != SeqAt(i + 1))
            j = next[j];

        i = i + 1;
        j = j + 1;

        if (j == s_len) {
            if (i - s_len + 1 >= end)
                break;

            res++;
            j = next[j];
        }
    }

    return res;
}


int main()
{
    int number_of_nodes = NumberOfNodes();
    int my_node_id = MyNodeId();

    long long s_len = SignalLength();
    long long m_len = SeqLength();

    //
    // One Thread Version
    //
    if (m_len < 1000)
    {
        if (my_node_id != 0)
            return 0;

        std::cout << searchKMP(s_len, m_len, 1, m_len - s_len + 1) << std::endl;
        return 0;
    }

    //
    // Many Threads Version
    //
    long long start = 0;
    long long end = 0;

    long long temp = (m_len - s_len + 1) / number_of_nodes;

    if (my_node_id == number_of_nodes - 1)
    {
        start = temp * my_node_id + 1;
        end = m_len - s_len + 1;
    }
    else
    {
        start = temp * my_node_id + 1;
        end = temp * (my_node_id + 1) + 1;
    }

    //
    s_len <= 10000000LL
            ? PutLL(0, searchKMP(s_len, m_len, start, end))
            : PutLL(0, search(s_len, m_len, start, end))
            ;
    Send(0);

    //
    if (my_node_id == 0)
    {
        long long p = 0;

        for (int i = 0; i < number_of_nodes; ++i) {
            Receive(i);
            p += GetLL(i);
        }

        std::cout << p << std::endl;
    }

    return 0;
}