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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* 2015
 * Maciej Szeptuch
 * II UWr
 *
 * Based on:
 *  On maximal suffixes and constant-space linear-time versions of KMP algorithm
 *  by Wojciech Rytter
 */
#include <algorithm>

#define NDEBUG

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

typedef unsigned int uint;

void runServer(void);
void runWorker(void);

void longestSelfMaxPrefix(uint &k, uint &period, const uint i, const uint j);
void maxSufAndPeriod(uint &v, uint &period, const uint j);
int approximatePeriod(const uint j);
bool isPeriod(const uint period, const uint j);

uint node;
uint nodes;
uint sequence;
uint signal;

int main(void)
{
    node    = MyNodeId();
    nodes   = NumberOfNodes();

    signal      = SignalLength();
    sequence    = SeqLength();

    nodes   = std::min(nodes, sequence - signal + 1);

    // Too many workers
    if(node >= nodes)
        return 0;

    if(!node)
        runServer();

    else
        runWorker();

    return 0;
}

/******************************************************************************
 * SERVER
 *****************************************************************************/
void runServer(void)
{
#ifndef NDEBUG
    assert(nodes > 0 && "Invalid number of nodes");
    fprintf(stderr, "Running server\n");
    fprintf(stderr, "    Nodes:     %u\n", nodes);
    fprintf(stderr, "    Signal:    %u\n", signal);
    fprintf(stderr, "    Sequence:  %u\n", sequence);
#endif // NDEBUG
    runWorker();

    uint result = 0U;
    for(uint n = 0; n < nodes; ++ n)
    {
        int source = Receive(-1);
        uint nodeResult = GetInt(source);
#ifndef NDEBUG
        fprintf(stderr, "Result from %u: %u\n", source, nodeResult);
#endif // NDEBUG
        result += nodeResult;
    }

    printf("%u\n", result);
}

/******************************************************************************
 * WORKER
 *****************************************************************************/
void runWorker(void)
{
#ifndef NDEBUG
    fprintf(stderr, "Running %u worker\n", node);
#endif // NDEBUG
    uint result = 0U;

    uint size = (sequence - signal + nodes) / nodes;
    uint start = node * size;
    uint end = std::min((node + 1) * size, sequence - signal + 1);

#ifndef NDEBUG
    fprintf(stderr, "%u: Processing [%u, %u)\n", node, start, end);
#endif // NDEBUG

    int period = signal;
    for(uint i = start, j = 0, R = 0; i < end;)
    {
        while(j < signal && SignalAt(j + 1) == SeqAt(i + j + 1))
            ++ j;

        if(j == signal) ++ result;
        if(period == -1 || (j < 2 * period || j > R))
        {
            period = approximatePeriod(j);
            R = j;
        }

        if(period == -1)
        {
            i += (j + 2) / 2;
            j = 0;
        }
        else
        {
            i += std::max(1, period);
            j -= period;
        }
    }

    PutInt(0, result);
#ifndef NDEBUG
    fprintf(stderr, "Sending %u result: %u\n", node, result);
#endif // NDEBUG
    Send(0);
}

void longestSelfMaxPrefix(uint &k, uint &period, const uint i, const uint j)
{
    period = 1;
    for(uint l = 2; l < j - i + 1; ++ l)
    {
        uint sig = SignalAt(i + l);
        uint sig2 = SignalAt(i + l - period);
        if(sig < sig2)
            period = l;

        else if(sig > sig2)
        {
            k = l - 1;
            return;
        }
    }

    k = j - i + 1;
}

void maxSufAndPeriod(uint &v, uint &period, const uint j)
{
    uint i = 1;
    uint k = 0;
    while(true)
    {
        longestSelfMaxPrefix(k, period, i, j);
        if(k == j - i + 1)
        {
            v = j - i + 1;
            return;
        }

        i += k - (k % period);
    }
}

int approximatePeriod(const uint j)
{
    uint period = 0;
    uint v      = 0;
    maxSufAndPeriod(v, period, j);

    if(v >= j / 2 and isPeriod(period, j))
        return period;

    return -1;
}

bool isPeriod(const uint period, const uint j)
{
    for(uint i = 1; i <= j - period; ++ i)
        if(SignalAt(i) != SignalAt(i + period))
            return false;

    return true;
}