#define debug_printf(fmt, ...) //#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0) #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <vector> #include <set> #include <map> #include <deque> #include <algorithm> #include <functional> #include <random> #include <limits> #include <iostream> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); #include "poszukiwania.h" #include "message.h" const int P_len_max = 20000000; int P[P_len_max + 1]; void kmp(int signal_from, int signal_size, int seq_from, int seq_size, std::set<int>& result) { result.clear(); int i; int j,t; P[0]=0; P[1]=0; t=0; for (j=2; j<=signal_size; j++) { while ((t>0)&&(SignalAt(signal_from + t)!=SignalAt(signal_from + j-1))) t=P[t]; if (SignalAt(signal_from + t)==SignalAt(signal_from + j-1)) t++; P[j]=t; } i=1; j=0; while (i<=seq_size-signal_size+1) { j=P[j]; while((j<signal_size)&&(SignalAt(signal_from + j)==SeqAt(seq_from + i+j-1))) j++; if (j==signal_size) { result.insert(i); } i=i+std::max(1,j-P[j]); } } int main() { int S = SignalLength(); int M = SeqLength(); int last_or_size = M - S + 1; int chunk = last_or_size / NumberOfNodes() + 1; int from = MyNodeId() * chunk + 1; int to = from + chunk - 1; if (to > last_or_size) { to = last_or_size; } int count = 0; if (from <= to) { std::set<int> possible; std::set<int> found; for (int signal_from=1; signal_from <= S; signal_from += P_len_max) { int signal_to = signal_from + P_len_max - 1; if (signal_to > S) { signal_to = S; } int signal_size = signal_to - signal_from + 1; int remaining_size = S - signal_to; int seq_from = from + signal_from - 1; int seq_to = to + S - 1 - remaining_size; int seq_size = seq_to - seq_from + 1; kmp(signal_from, signal_size, seq_from, seq_size, found); if (found.size() == 0) { break; } if (signal_from != 1) { for (auto it=possible.begin(); it != possible.end(); ) { if (0 == found.count(*it)) { it = possible.erase(it); } else { ++it; } } if (possible.size() == 0) { break; } } else { possible.insert(found.begin(), found.end()); //std::cout << possible.size() << std::endl; } } count += possible.size(); } if (MyNodeId() == 0) { for (int node=1; node < NumberOfNodes(); ++node) { Receive(node); count += GetInt(node); } std::cout << count << std::endl; } else { PutInt(0, count); Send(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 | #define debug_printf(fmt, ...) //#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0) #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <vector> #include <set> #include <map> #include <deque> #include <algorithm> #include <functional> #include <random> #include <limits> #include <iostream> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); #include "poszukiwania.h" #include "message.h" const int P_len_max = 20000000; int P[P_len_max + 1]; void kmp(int signal_from, int signal_size, int seq_from, int seq_size, std::set<int>& result) { result.clear(); int i; int j,t; P[0]=0; P[1]=0; t=0; for (j=2; j<=signal_size; j++) { while ((t>0)&&(SignalAt(signal_from + t)!=SignalAt(signal_from + j-1))) t=P[t]; if (SignalAt(signal_from + t)==SignalAt(signal_from + j-1)) t++; P[j]=t; } i=1; j=0; while (i<=seq_size-signal_size+1) { j=P[j]; while((j<signal_size)&&(SignalAt(signal_from + j)==SeqAt(seq_from + i+j-1))) j++; if (j==signal_size) { result.insert(i); } i=i+std::max(1,j-P[j]); } } int main() { int S = SignalLength(); int M = SeqLength(); int last_or_size = M - S + 1; int chunk = last_or_size / NumberOfNodes() + 1; int from = MyNodeId() * chunk + 1; int to = from + chunk - 1; if (to > last_or_size) { to = last_or_size; } int count = 0; if (from <= to) { std::set<int> possible; std::set<int> found; for (int signal_from=1; signal_from <= S; signal_from += P_len_max) { int signal_to = signal_from + P_len_max - 1; if (signal_to > S) { signal_to = S; } int signal_size = signal_to - signal_from + 1; int remaining_size = S - signal_to; int seq_from = from + signal_from - 1; int seq_to = to + S - 1 - remaining_size; int seq_size = seq_to - seq_from + 1; kmp(signal_from, signal_size, seq_from, seq_size, found); if (found.size() == 0) { break; } if (signal_from != 1) { for (auto it=possible.begin(); it != possible.end(); ) { if (0 == found.count(*it)) { it = possible.erase(it); } else { ++it; } } if (possible.size() == 0) { break; } } else { possible.insert(found.begin(), found.end()); //std::cout << possible.size() << std::endl; } } count += possible.size(); } if (MyNodeId() == 0) { for (int node=1; node < NumberOfNodes(); ++node) { Receive(node); count += GetInt(node); } std::cout << count << std::endl; } else { PutInt(0, count); Send(0); } } |