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
#include <cstdio>
#include <algorithm>
#include "poszukiwania.h"
#include "message.h"
using namespace std;

int nodes;
int node;
long long int words;

long long int signalLength;
long long int seqLength;

long long int *signal;
long long int *p;

inline void buildSignalTable()
{
	long long int t = -1;
	p[0] = -1;
	for (long long int j=1; j<=signalLength; ++j)
	{
		while(t >= 0 && (signal[t+1] != signal[j]))
			t = p[t];
		t++;
		p[j] = t;
	}
}

inline void solve(const long long int from, const long long int to)
{
	//printf("SOLVE %lld %lld\n", from, to);

	if (from > to)
		return ;
	signal = new long long int[signalLength + 2];
	p = new long long int[signalLength + 2];
	
	for (long long int i = 1; i <= signalLength; ++i)
		signal[i] = SignalAt(i);

	buildSignalTable();

	//for (long long int i = 1; i <= signalLength; ++i)
	//	printf("%3lld ", signal[i]);
	//printf("\n");
	//for (long long int i = 1; i <= signalLength; ++i)
	//	printf("%3lld ", p[i-1]);
	//printf("\n");
	
	long long int i = from, j = 0;
	
	while (i <= to)
	{
		//printf("%lld %lld\n", i, j);
		while (j < signalLength && signal[j+1] == SeqAt(i + j))
		{
			//printf("%lld\n", j);
			j++;
		}
		//printf("%lld %lld\n", i, j);
		if (j == signalLength)
			words++;
		//	printf("X: %lld\n", i);
		i = i + j - p[j];
		j = max((long long int) 0, p[j]);
		//printf("%lld %lld\n", i, j);
	}
  
	delete signal;
	delete p;
}

inline void printSolution()
{
	for (int i = 1; i < nodes; ++i) 
	{
		Receive(i);
		words += GetLL(i);
    }
	printf("%lld\n", words);
}

inline void sendSolution()
{
	//printf("SEND\n");
	PutLL(0, words);
	Send(0);
}

int main()
{
	nodes = NumberOfNodes();
	node = MyNodeId();
	
	signalLength = SignalLength();
	seqLength = SeqLength();

	if (node == 0) //TROLOL
	{
		solve(1, seqLength - signalLength + 1);
		printf("%lld\n", words);
		return 0;
	}
	//printf("SIGNAL: %lld\n", signalLength);
	//printf("SEQ: %lld\n", seqLength);

	//printf("NODES: %d\n", nodes);
	//printf("NODE: %d\n", node);
	
	if (signalLength == seqLength)
	{
		if (node == 0)
		{
			solve(1, 1);
			printf("%lld\n", words);
		}
		return 0;
	}
	else if (seqLength - signalLength + 1 < nodes)
	{
		if (node == 0)
		{
			solve(1, 1 + seqLength - signalLength + 1);
			printf("%lld\n", words);
		}
		return 0;
	}
	else
	{
		//printf("LOL\n");
		long long int toDo = seqLength - signalLength + 1;
		long long int diff = toDo / nodes;
		if (toDo % nodes != 0)
			 diff ++;
		//printf("%lld\n", diff);
		solve(1 + diff * node, min(toDo, diff * (node + 1)));
	}
		
	if (node == 0)
		printSolution();
	else
		sendSolution();
	return 0;
}