We will divide the sequence on min(n, p) sequences. 1-st, (1 + p)-th, (1 + 2·p)-th, ... element will go to the first sequence, 2-nd,(2 + p)-th, (2 + 2·p)-th... will go to the second sequence and so on. Now you need to find an answer for each of them, considering thatp = 1. This can be solved by a simple method. You can go along the sequence from left to right and count the number of occurrences of each number. If the number of occurrences of each number will match the number of occurrences of the same number in the second sequence, then everything is OK.
FOR COUNTING ME CAN USE MAP FIRST MAP THE 2ND SEQUENCE AND THAN
GO THROUGH EACH SEQUENCE AND INITIALLY DIFFERENCE IS M AMONG BOTH MAP
NOW INSET 1 ELEMENT AND IF SIZE OF SLID IS> M REMOVE 1 ELEMENT FROM THE LEFT CORNET IF AFTER THESE 2 OPERATION DIFFERENCE COUNT =0 THIS IS A VALID SEGMENT .....................
---------------------------------------------------CODE-------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
map<lli,int> ma1,ma2;
lli arr[1000000];
vector<int> idx;
vector<int> tidx;
vector<lli> temp;
int main()
{
int n,m,k;
scanf("%d %d %d",&n,&m,&k);
for(int i=0;i<n;i++)
{
scanf("%lld",&arr[i]);
}
for(int i=0;i<m;i++)
{
lli a;
scanf("%lld",&a);
ma2[a]++;
}
lli ans=0;
for(int i=0;i<k;i++)
{
temp.clear();
tidx.clear();
for(int j=i;j<n;j+=k)
{
temp.push_back(arr[j]);
tidx.push_back(j);
}
int sz=temp.size();
int dif=m;
ma1.clear();
for(int j=0;j<m && j<sz;j++)
{
ma1[temp[j]]++;
if(ma1[temp[j]]<=ma2[temp[j]]) dif--;
//else dif++;
}
if(dif==0)
{
ans++;
idx.push_back(i);
}
for(int j=m;j<sz;j++)
{
ma1[temp[j-m]]--;
if(ma1[temp[j-m]]<ma2[temp[j-m]]) dif++;
ma1[temp[j]]++;
if(ma1[temp[j]]<=ma2[temp[j]]) dif--;
if(dif==0)
{
ans++;
idx.push_back(tidx[j-m+1]);
}
}
}
cout<<ans<<endl;
sort(idx.begin(),idx.end());
for(int i=0;i<ans;i++)
cout<<idx[i]+1<<" ";
}