Advertisement
Guest User

Untitled

a guest
May 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using pii=pair<int,int>;
  5. using ll=long long;
  6. const int N=2e5+10;
  7. int a[N],b[N];
  8. int n,m;
  9. const ll PB=98765431;
  10.  
  11. int main()
  12. {
  13. scanf("%d%d",&n,&m);
  14. for(int i=1;i<=n;++i) scanf("%d",&a[i]);
  15. for(int i=1;i<=m;++i) scanf("%d",&b[i]);
  16. ll hsh=0,h=0,rem=1;
  17. vector <int> idx;
  18. vector <long long> pb;
  19. for(int i=1;i<=m;++i){
  20. if(i!=1) rem *= PB;
  21. if(b[i]==1){
  22. idx.push_back(i);
  23. hsh = hsh*PB;
  24. }else{
  25. hsh = hsh*PB + (ll)b[i];
  26. }
  27. pb.push_back(rem);
  28. }
  29. int cnt=0;
  30. for(int i=1;i<=n;++i){
  31. h = h*PB + (ll)a[i];
  32. if(i>=m){
  33. ll ch=h;
  34. for(auto x:idx)
  35. ch -= (ll)a[i-m+x] * pb[m-x];
  36. if(ch==hsh) cnt++;
  37. h -= rem * (ll)a[i-m+1];
  38. }
  39. }
  40. printf("%d",cnt);
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement