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

using namespace std;

const int maxl = 1e7;
int a[maxl];
int p[maxl];

int main() {
	ios_base::sync_with_stdio(0);

	if ( MyNodeId() != 0 ) {
		return 0;
	}

  	int s = SignalLength();
	int m = SeqLength();

	for (int i = 0; i < s; i++) {
		a[i] = SignalAt(i+1);
	}

	a[s] = -1;

	for (int i = s+1; i < s+m+1; i++) {
		a[i] = SeqAt(i-s);
	}

	p[0] = 0;

	int d = 0, r = 0;

	for (int i = 1; i < s+m+1; i++) {
		while (d > 0 && a[d] != a[i]) {
			d = p[d - 1];
		}
		if (a[d] == a[i])
			d++;
		p[i] = d;

		if (d == s) r++;
	}

	cout << r << endl;

	return 0;
}