#include "poszukiwania.h" #include "message.h" #include <algorithm> #include <iostream> #include <vector> #include <utility> using namespace std; #define PB push_back #define ST first #define ND second #define MP make_pair typedef long long LL; LL N, M, B; int Bb, Be; int my_id, nodes; LL CeilDiv(LL a, LL b) { return (a + b - 1) / b; } vector<int> pi; // For KMP algorighm vector<int> matches; void KMPPrefix() { int k = -1; pi.PB(-1); for (int q = Bb; q < Be; ++q) { while (k >= 0 && SignalAt(Bb + k + 1 + 1) != SignalAt(q + 1)) k = pi[k]; if (SignalAt(Bb + k + 1 + 1) == SignalAt(q + 1)) ++k; pi.PB(k); } } void SearchForSignal() { KMPPrefix(); int q = -1; int m = Be - Bb; for (int i = Bb; i < N - (M - Be); ++i) { while (q >= 0 && SignalAt(Bb + q + 1 + 1) != SeqAt(i + 1)) q = pi[q]; if (SignalAt(Bb + q + 1 + 1) == SeqAt(i + 1)) ++q; //if (i - Bb > m && q <= i - Bb - m + 1) // break; if (q == m - 1) { matches.PB(i - Bb - m + 1); q = pi[q]; } } //cerr << "matches: |" << matches.size() << "|"; //for (auto m: matches) cerr << " " << m; //cerr << endl; } void FilterMatches(int p, vector<int>& mch) { mch.clear(); Receive(p); int n = GetInt(p); for (int i = 0; i < n; ++i) { int m = GetInt(p); if (binary_search(begin(matches), end(matches), m)) mch.PB(m); } } void SendMatches(int nd, const vector<int>& mch) { PutInt(nd, (int) mch.size()); for (auto v: mch) PutInt(nd, v); Send(nd); } void MergeResults() { std::vector<int> mch; int an = CeilDiv(M, B); while (an >= 2) { int an2 = CeilDiv(an, 2); if (my_id >= an2) { SendMatches(my_id - an2, matches); return; } else if (my_id + an2 < an) { FilterMatches(my_id + an2, mch); swap(matches, mch); } an = an2; } cout << matches.size() << "\n"; } int main() { N = SeqLength(); M = SignalLength(); my_id = MyNodeId(); nodes = NumberOfNodes(); B = CeilDiv(M, nodes); Bb = (int)(my_id * B); Be = (int)min((my_id + 1) * B, M); if (Bb >= Be) return 0; SearchForSignal(); MergeResults(); return 0; }
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 | #include "poszukiwania.h" #include "message.h" #include <algorithm> #include <iostream> #include <vector> #include <utility> using namespace std; #define PB push_back #define ST first #define ND second #define MP make_pair typedef long long LL; LL N, M, B; int Bb, Be; int my_id, nodes; LL CeilDiv(LL a, LL b) { return (a + b - 1) / b; } vector<int> pi; // For KMP algorighm vector<int> matches; void KMPPrefix() { int k = -1; pi.PB(-1); for (int q = Bb; q < Be; ++q) { while (k >= 0 && SignalAt(Bb + k + 1 + 1) != SignalAt(q + 1)) k = pi[k]; if (SignalAt(Bb + k + 1 + 1) == SignalAt(q + 1)) ++k; pi.PB(k); } } void SearchForSignal() { KMPPrefix(); int q = -1; int m = Be - Bb; for (int i = Bb; i < N - (M - Be); ++i) { while (q >= 0 && SignalAt(Bb + q + 1 + 1) != SeqAt(i + 1)) q = pi[q]; if (SignalAt(Bb + q + 1 + 1) == SeqAt(i + 1)) ++q; //if (i - Bb > m && q <= i - Bb - m + 1) // break; if (q == m - 1) { matches.PB(i - Bb - m + 1); q = pi[q]; } } //cerr << "matches: |" << matches.size() << "|"; //for (auto m: matches) cerr << " " << m; //cerr << endl; } void FilterMatches(int p, vector<int>& mch) { mch.clear(); Receive(p); int n = GetInt(p); for (int i = 0; i < n; ++i) { int m = GetInt(p); if (binary_search(begin(matches), end(matches), m)) mch.PB(m); } } void SendMatches(int nd, const vector<int>& mch) { PutInt(nd, (int) mch.size()); for (auto v: mch) PutInt(nd, v); Send(nd); } void MergeResults() { std::vector<int> mch; int an = CeilDiv(M, B); while (an >= 2) { int an2 = CeilDiv(an, 2); if (my_id >= an2) { SendMatches(my_id - an2, matches); return; } else if (my_id + an2 < an) { FilterMatches(my_id + an2, mch); swap(matches, mch); } an = an2; } cout << matches.size() << "\n"; } int main() { N = SeqLength(); M = SignalLength(); my_id = MyNodeId(); nodes = NumberOfNodes(); B = CeilDiv(M, nodes); Bb = (int)(my_id * B); Be = (int)min((my_id + 1) * B, M); if (Bb >= Be) return 0; SearchForSignal(); MergeResults(); return 0; } |