#include <cstdio> #include <iostream> #include <vector> #include <list> #include <string> #include <set> #include <map> #include <algorithm> #include <utility> #include <cmath> #include <queue> #include <stack> #include <cassert> #include <cstring> #include <climits> #include <functional> #include <unordered_set> #include <unordered_map> #define VAR(i,v) __typeof(v) i = (v) #define SIZE(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define REP(i,b) for(int i=0; i<(b); ++i) #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 FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define PB push_back #define MP make_pair #define ST first #define ND second #define NL printf("\n") #include "message.h" #include "poszukiwania.h" using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef long long LL; const int INF = 2147483640; const int MOD = 1000000007; const int MAXN = 1000005; int main() { int n = SignalLength(); int m = SeqLength(); VI b(m); REP(i,m) b[i] = SeqAt(i+1); int p = 1; REP(i,n) p *= MOD; int h = SignalAt(1); FOR(i,1,n-1) h = h*MOD + SignalAt(i+1); VI hsh(m); hsh[0] = b[0]; FOR(i,1,m-1) hsh[i] = hsh[i-1]*MOD + b[i]; auto get_hash = [&](int x, int y) { return hsh[y] - (x ? hsh[x-1]*p : 0); }; int res = 0; REP(i,m-n+1) if(get_hash(i,i+n-1) == h) res++; if(MyNodeId() == 0) printf("%d\n", res); 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 | #include <cstdio> #include <iostream> #include <vector> #include <list> #include <string> #include <set> #include <map> #include <algorithm> #include <utility> #include <cmath> #include <queue> #include <stack> #include <cassert> #include <cstring> #include <climits> #include <functional> #include <unordered_set> #include <unordered_map> #define VAR(i,v) __typeof(v) i = (v) #define SIZE(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define REP(i,b) for(int i=0; i<(b); ++i) #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 FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i) #define PB push_back #define MP make_pair #define ST first #define ND second #define NL printf("\n") #include "message.h" #include "poszukiwania.h" using namespace std; typedef pair<int,int> PII; typedef vector<int> VI; typedef long long LL; const int INF = 2147483640; const int MOD = 1000000007; const int MAXN = 1000005; int main() { int n = SignalLength(); int m = SeqLength(); VI b(m); REP(i,m) b[i] = SeqAt(i+1); int p = 1; REP(i,n) p *= MOD; int h = SignalAt(1); FOR(i,1,n-1) h = h*MOD + SignalAt(i+1); VI hsh(m); hsh[0] = b[0]; FOR(i,1,m-1) hsh[i] = hsh[i-1]*MOD + b[i]; auto get_hash = [&](int x, int y) { return hsh[y] - (x ? hsh[x-1]*p : 0); }; int res = 0; REP(i,m-n+1) if(get_hash(i,i+n-1) == h) res++; if(MyNodeId() == 0) printf("%d\n", res); return 0; } |