Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of mintegers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q(q + (m - 1)·p ≤ n; q ≥ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements.
Sereja needs to rush to the gym, so he asked to find all the described positions of q.
The first line contains three integers n, m and p (1 ≤ n, m ≤ 2·105, 1 ≤ p ≤ 2·105). The next line contains n integers a1, a2, ..., an(1 ≤ ai ≤ 109). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 109).
In the first line print the number of valid qs. In the second line, print the valid values in the increasing order.
5 3 1 1 2 3 2 1 1 2 3
2 1 3
6 3 2 1 3 2 2 3 1 1 2 3
21 2
题意:给出数组a,b,然后请你以给出的q为公差,使得下标为i,i+p,i+2*p,,,这些数组成的数列跟b数列相等,求所有有可能的起点i
思路:我们枚举每一个可能的i,然后往后查找,一旦找够m个数,就跟b数组进行比较,这里我们把找到的数,还有b数组分别存放在两个map中,然后判断的时候直接看map是否相等就可以了,相等的话记录第一个数的下标,但是不管找到的数组跟b数组是否相等,我们都应该去除第一个数的下标,保证可以有新的数进入map,在进行弹栈的时候都应该判断一下操作的数字出现次数是否为0,如果为零就即使清理,因为map讲究的是一一对应,如果不进行清除,在有的数据中会有错误
#include<cstdio> #include<cstring> #include<map> #include<queue> #include<algorithm> using namespace std; int a[1000000+10]; bool vis[1000000+10]; int n,m,p,ans; map<int,int>tp,fp; void solve(int s) { tp.clear(); queue<int>q; for(int i=s;i<=n;i+=p) { q.push(i); tp[a[i]]++; if(q.size()==m) { if(fp==tp) { vis[q.front()]=true; ans++; } int v=a[q.front()]; q.pop(); if(--tp[v]==0) tp.erase(v); } } } int main() { while(scanf("%d%d%d",&n,&m,&p)!=EOF) { memset(vis,false,sizeof(vis)); memset(a,0,sizeof(a)); fp.clear(); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=0;i<m;i++) { int b; scanf("%d",&b); fp[b]++; } ans=0; for(int i=1;i<=p;i++) solve(i); printf("%d\n",ans); int cnt=0; for(int i=1;i<=n;i++) { if(vis[i]) { if(cnt>0) printf(" "); cnt++; printf("%d",i); } } if(ans) printf("\n"); } return 0; }