//============================================================================ // Name : .cpp // Author : Grzegorz Gajoch // Description : //============================================================================ #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> #include <ctype.h> #include <algorithm> #include <string> #include <string.h> #include <cstring> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <list> using namespace std; #define FOR(i,a,b) for(int i = a; i <= b; ++i) #define FORD(i,a,b) for(int i = a; i >= b; --i) #define REP(x, n) for(int x=0; x < (n); ++x) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define SIZE(n) (int)n.size() #define ALL(c) c.begin(),c.end() #define PB(n) push_back(n) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<vector<int> > VVI; typedef vector<bool> VB; #define MP make_pair #define ST first #define ND second const int INF = 2000000005; #undef DEBUG #ifdef DEBUG #define debug printf #else #define debug #endif #include "message.h" #include "poszukiwania.h" vector<int> T; long long KMP(long long Begin, long long End) { // K - signal // S - seq long long SigLen = SignalLength(); long long SeqLen = End-Begin; T.resize(SigLen + 1, -1); long long matches = 0; if( SeqLen < SigLen ) return 0; for(int i = 1; i <= SigLen; i++) { int pos = T[i - 1]; while(pos != -1 && SignalAt(pos+1) != SignalAt(i - 1+1)) pos = T[pos]; T[i] = pos + 1; } int sp = 0; int kp = 0; while(sp < SeqLen) { while(kp != -1 && (kp == SigLen || SignalAt(1+kp) != SeqAt(sp+Begin))) kp = T[kp]; kp++; sp++; if(kp == SigLen) matches++; } return matches; } int main(int argc, char **argv) { long long M = SeqLength(); long long S = SignalLength(); long long Length = SeqLength()/NumberOfNodes(); if( Length < S ) { Length = S; } debug("length: %d\n",Length); //REP(xxxxxxxx, NumberOfNodes()) { long long Begin = MyNodeId() * Length+1; long long End = Begin + Length + S - 1; Begin = min(Begin, M+1); End = min(End, M+1); debug("%d %d\n",Begin, End); long long x = KMP(Begin, End); debug("matches: %d\n", x); //} PutLL(0, x); Send(0); if( MyNodeId() == 0 ) { long long m = 0; REP(i, NumberOfNodes()) { Receive(i); m += GetLL(i); } printf("%lld\n", m); } return 0; //if( begin == end ) { // 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 119 120 121 122 123 124 | //============================================================================ // Name : .cpp // Author : Grzegorz Gajoch // Description : //============================================================================ #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> #include <ctype.h> #include <algorithm> #include <string> #include <string.h> #include <cstring> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <list> using namespace std; #define FOR(i,a,b) for(int i = a; i <= b; ++i) #define FORD(i,a,b) for(int i = a; i >= b; --i) #define REP(x, n) for(int x=0; x < (n); ++x) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define SIZE(n) (int)n.size() #define ALL(c) c.begin(),c.end() #define PB(n) push_back(n) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<vector<int> > VVI; typedef vector<bool> VB; #define MP make_pair #define ST first #define ND second const int INF = 2000000005; #undef DEBUG #ifdef DEBUG #define debug printf #else #define debug #endif #include "message.h" #include "poszukiwania.h" vector<int> T; long long KMP(long long Begin, long long End) { // K - signal // S - seq long long SigLen = SignalLength(); long long SeqLen = End-Begin; T.resize(SigLen + 1, -1); long long matches = 0; if( SeqLen < SigLen ) return 0; for(int i = 1; i <= SigLen; i++) { int pos = T[i - 1]; while(pos != -1 && SignalAt(pos+1) != SignalAt(i - 1+1)) pos = T[pos]; T[i] = pos + 1; } int sp = 0; int kp = 0; while(sp < SeqLen) { while(kp != -1 && (kp == SigLen || SignalAt(1+kp) != SeqAt(sp+Begin))) kp = T[kp]; kp++; sp++; if(kp == SigLen) matches++; } return matches; } int main(int argc, char **argv) { long long M = SeqLength(); long long S = SignalLength(); long long Length = SeqLength()/NumberOfNodes(); if( Length < S ) { Length = S; } debug("length: %d\n",Length); //REP(xxxxxxxx, NumberOfNodes()) { long long Begin = MyNodeId() * Length+1; long long End = Begin + Length + S - 1; Begin = min(Begin, M+1); End = min(End, M+1); debug("%d %d\n",Begin, End); long long x = KMP(Begin, End); debug("matches: %d\n", x); //} PutLL(0, x); Send(0); if( MyNodeId() == 0 ) { long long m = 0; REP(i, NumberOfNodes()) { Receive(i); m += GetLL(i); } printf("%lld\n", m); } return 0; //if( begin == end ) { // send 0 //} } |