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

using namespace std;

void Pref(vector <int> &P, string &S){
	unsigned int t=0, n=S.size();
	P.resize(n+1, 0);
	for(unsigned int i=2; i<n; ++i){
		while(t>0 && S[t+1]!=S[i])
			t=P[t];
		if(S[t+1]==S[i])
			++t;
		P[i]=t;
	}
}

long long int KMP(string &T, string &W){
	string S="#"+W+"#"+T;
	long long int wynik=0;
	vector <int> P;
	Pref(P, S);
	unsigned int ws=W.size();
	for(unsigned int i=ws+2; i<S.size(); ++i){
		if(P[i]==ws)
			++wynik;
	}
	return wynik;
}

int main() {
	string T, W;
	for(int i=1; i<=SeqLength(); ++i){
		T.push_back(SeqAt(i));
	}
	for(int i=1; i<=SignalLength(); ++i){
		W.push_back(SignalAt(i));
	}
	if(MyNodeId()==1)cout<<KMP(T,W)<<endl;
	return 0;
}