#include "poszukiwania.h" #include "message.h" #include <stdint.h> #include <algorithm> #include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t n = SeqLength(); int64_t m = SignalLength(); int left = 0; int right = n - m; int localLeft = (right + 1) * int64_t(MyNodeId()) / NumberOfNodes(); int localRight = (right + 1) * int64_t(MyNodeId() + 1) / NumberOfNodes() - 1; int result = 0; if(m > 30000000) { for(int offset = localLeft; offset <= localRight; ++offset) { bool ok = true; for(int i = 0; i < m; ++i) { if(SignalAt(1 + i) != SeqAt(1 + offset + i)) { ok = false; break; } } result += ok; } } else { int *pmt = new int[m + 1]; { pmt[0] = -1; pmt[1] = 0; for(int i = 2, s = 0; i <= m; ++i) { while(SignalAt(s + 1) != SignalAt(i) and s != 0) s = pmt[s]; s += (SignalAt(s + 1) == SignalAt(i)); pmt[i] = s; } } { for(int i = localLeft, s = 0; i <= localRight + m - 1; ++i) { while(SignalAt(s + 1) != SeqAt(i + 1) and s != 0) s = pmt[s]; s += (SeqAt(i + 1) == SignalAt(s + 1)); if(s == m) { ++result; s = pmt[s]; } } } delete[] pmt; } if(MyNodeId() != 0) { PutInt(0, result); Send(0); } else { for(int node = 1; node < NumberOfNodes(); ++node) { Receive(node); result += GetInt(node); } cout << result << '\n'; } 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 | #include "poszukiwania.h" #include "message.h" #include <stdint.h> #include <algorithm> #include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t n = SeqLength(); int64_t m = SignalLength(); int left = 0; int right = n - m; int localLeft = (right + 1) * int64_t(MyNodeId()) / NumberOfNodes(); int localRight = (right + 1) * int64_t(MyNodeId() + 1) / NumberOfNodes() - 1; int result = 0; if(m > 30000000) { for(int offset = localLeft; offset <= localRight; ++offset) { bool ok = true; for(int i = 0; i < m; ++i) { if(SignalAt(1 + i) != SeqAt(1 + offset + i)) { ok = false; break; } } result += ok; } } else { int *pmt = new int[m + 1]; { pmt[0] = -1; pmt[1] = 0; for(int i = 2, s = 0; i <= m; ++i) { while(SignalAt(s + 1) != SignalAt(i) and s != 0) s = pmt[s]; s += (SignalAt(s + 1) == SignalAt(i)); pmt[i] = s; } } { for(int i = localLeft, s = 0; i <= localRight + m - 1; ++i) { while(SignalAt(s + 1) != SeqAt(i + 1) and s != 0) s = pmt[s]; s += (SeqAt(i + 1) == SignalAt(s + 1)); if(s == m) { ++result; s = pmt[s]; } } } delete[] pmt; } if(MyNodeId() != 0) { PutInt(0, result); Send(0); } else { for(int node = 1; node < NumberOfNodes(); ++node) { Receive(node); result += GetInt(node); } cout << result << '\n'; } return 0; } |