#include "message.h" #include "poszukiwania.h" #include <cstdio> #include <vector> using namespace std; long long get_num(long long id) { if (id < SignalLength()) return SignalAt(id+1); if (id == SignalLength()) return 2*((long long)(1000))*((long long)(1000)) + 2; if (id - SignalLength() - 1 < SeqLength()) return SeqAt(id - SignalLength()); return 2*((long long)(1000))*((long long)(1000)) + 3; } int main() { if (MyNodeId() != 0) return 0; int result = 0; vector<int> T(SignalLength()+1+SeqLength()+1); T[0] = -1; for(int i=0; i<T.size()-1; ++i) { T[i+1] = T[i] + 1; while (T[i+1]>0 && get_num(i)!=get_num(T[i+1]-1)) T[i+1] = T[T[i+1]-1]+1; if (T[i+1] == SignalLength()) ++result; } printf("%d\n", result); return 0; } // KMP wziety ze strony https://www.ics.uci.edu/~eppstein/161/960227.html
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 | #include "message.h" #include "poszukiwania.h" #include <cstdio> #include <vector> using namespace std; long long get_num(long long id) { if (id < SignalLength()) return SignalAt(id+1); if (id == SignalLength()) return 2*((long long)(1000))*((long long)(1000)) + 2; if (id - SignalLength() - 1 < SeqLength()) return SeqAt(id - SignalLength()); return 2*((long long)(1000))*((long long)(1000)) + 3; } int main() { if (MyNodeId() != 0) return 0; int result = 0; vector<int> T(SignalLength()+1+SeqLength()+1); T[0] = -1; for(int i=0; i<T.size()-1; ++i) { T[i+1] = T[i] + 1; while (T[i+1]>0 && get_num(i)!=get_num(T[i+1]-1)) T[i+1] = T[T[i+1]-1]+1; if (T[i+1] == SignalLength()) ++result; } printf("%d\n", result); return 0; } // KMP wziety ze strony https://www.ics.uci.edu/~eppstein/161/960227.html |