Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using pii=pair<int,int>;
- using ll=long long;
- const int N=2e5+10;
- int a[N],b[N];
- int n,m;
- const ll PB=98765431;
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=1;i<=n;++i) scanf("%d",&a[i]);
- for(int i=1;i<=m;++i) scanf("%d",&b[i]);
- ll hsh=0,h=0,rem=1;
- vector <int> idx;
- vector <long long> pb;
- for(int i=1;i<=m;++i){
- if(i!=1) rem *= PB;
- if(b[i]==1){
- idx.push_back(i);
- hsh = hsh*PB;
- }else{
- hsh = hsh*PB + (ll)b[i];
- }
- pb.push_back(rem);
- }
- int cnt=0;
- for(int i=1;i<=n;++i){
- h = h*PB + (ll)a[i];
- if(i>=m){
- ll ch=h;
- for(auto x:idx)
- ch -= (ll)a[i-m+x] * pb[m-x];
- if(ch==hsh) cnt++;
- h -= rem * (ll)a[i-m+1];
- }
- }
- printf("%d",cnt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement