思路:用数组来存储超时,学习了用map<int,int>的用法,可以直接比较
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int N = 2e5+10;
map<int,int> mchp,findp;
int cnt,n,m,p;
int ans[N];
int num[N];
void solve( int pos ) {
int count=0;
queue<int> q;
findp.clear();
for ( int i=pos ; i<=n; i+=p ) {
findp[num[i]]++;
++count;
q.push(i);
if ( count==m ) {
if ( findp==mchp ) {
ans[cnt++] = q.front();
}
--findp[ num[ q.front()]];
if ( findp[num[ q.front()]]==0 )findp.erase(num[q.front()]);//!!!!
q.pop();
--count;
}
}
}
int main()
{
int temp;
scanf("%d %d %d",&n,&m,&p);
for ( int i=1; i<=n; i++ ) scanf("%d",&num[i]);
for ( int i=1; i<=m; i++ ) {
scanf("%d",&temp);
mchp[temp]++;
}
for ( int i=1; i<=p; i++ )
solve( i ) ;
sort(ans,ans+cnt);
cout<<cnt<<endl;
for ( int i=0; i<cnt; i++ )
if ( i==cnt-1 ) printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
return 0;
}
间隔是p,即从1到p开始枚举,若长度超过了m,则剪掉最前面的数字