//Algorytm Knutha-Morrisa-Pratta na podstawie algorytmu Tomasza Lubinskiego z www.Algorytm.org #include "poszukiwania.h" #include "message.h" #include <iostream> using namespace std; int main() { if (MyNodeId() == 0) { int m, n, i, j, t; n = SeqLength(); m = SignalLength(); int P[m]; //obliczenie tablicy P P[0] = 0; P[1] = 0; t = 0; for (j = 2; j <= m; j++) { while ((t > 0) && (SignalAt(t + 1) != SignalAt(j))) t = P[t]; if (SignalAt(t + 1) == SignalAt(j)) t++; P[j] = t; } int result = 0; i = 1; j = 0; while (i <= n - m + 1) { j = P[j]; while ((j < m) && (SignalAt(j + 1) == SeqAt(i + j))) j++; if (j == m) result++; if (1 < j - P[j]) i = i + j - P[j]; else i++; } printf("%d\n", result); } }
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 | //Algorytm Knutha-Morrisa-Pratta na podstawie algorytmu Tomasza Lubinskiego z www.Algorytm.org #include "poszukiwania.h" #include "message.h" #include <iostream> using namespace std; int main() { if (MyNodeId() == 0) { int m, n, i, j, t; n = SeqLength(); m = SignalLength(); int P[m]; //obliczenie tablicy P P[0] = 0; P[1] = 0; t = 0; for (j = 2; j <= m; j++) { while ((t > 0) && (SignalAt(t + 1) != SignalAt(j))) t = P[t]; if (SignalAt(t + 1) == SignalAt(j)) t++; P[j] = t; } int result = 0; i = 1; j = 0; while (i <= n - m + 1) { j = P[j]; while ((j < m) && (SignalAt(j + 1) == SeqAt(i + j))) j++; if (j == m) result++; if (1 < j - P[j]) i = i + j - P[j]; else i++; } printf("%d\n", result); } } |