Sereja ans Anagrams

Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers 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.

Input
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).

Output
In the first line print the number of valid qs. In the second line, print the valid values in the increasing order.

Example
Input
5 3 1
1 2 3 2 1
1 2 3
Output
2
1 3
Input
6 3 2
1 3 2 2 3 1
1 2 3
Output
2

1 2

题意:给定n个元素的序列a[]和m个元素的序列b[],让你找到一个q使得a[q]、a[q+p]、…a[q+(m-1)*p]构成序列b。问你所有的q。

首先这个题不需要去管位置是否一一对应...只要找出的元素能构成B

思路: 首先我们需要明确只有1---p个位置是可能可行的位置,因为我们可以发现,

你比如p=2 n=10 m=3 那么有 a1+a3+a5a2+a4+a6 a3+a5+a7 a4+a6+a8 a5+a7+a9 a6+a8+a10.   

而我们可以发现  有一部分的数是重叠的, 在上面的例子就可以认为下标奇数的序列有一部分重叠,偶数的也是

我们为了避免这些重叠部分,我们就可以考虑用到队列,即每次取够了m个我们就和b比较 ,如果可以记录,不可以就把当前的出队,入后面的元素入队.那么只要有1---p这么多位置就可以把所有的情况找出来了...

这里还有一个技巧,我也是才知道...原来两个map还能直接比较是否相等,所以说如果当前队首元素的个数为0,要把这个元素从map中删除,否则两个map不等!(题意是网上找的,不过和我的想法是一样的)


#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int N=220000;
typedef map<int,int> mm;
int n,m,p,a[N],b[N];
mm B;
int main()
{
    while(~scanf("%d%d%d",&n,&m,&p))
    {
        B.clear();
        for(int i=1;i<=n;i++)
            scanf("%d",a+i);
       for(int i=0;i<m;i++)
        {
            int x;
            scanf("%d",&x);
            B[x]++;
        }
        if((m-1)*p+1>n)
        {
            printf("0\n");
            continue;
        }
         int h=0;
        for(int i=1;i<=p;i++)//不同的间隔起点
        {
            mm A;
            A.clear();
            deque<int>q;
            for(int j=i;j<=n;j+=p)
            {
                q.push_back(j);
                A[a[j]]++;
                if(q.size()>m)
                {
                    int w=q.front();
                    q.pop_front();
                    A[a[w]]--;
                    if(A[a[w]]==0)//记得删除,不然比较时不相等
                        A.erase(a[w]);

                }
                if(A==B)//map可以直接比较是否相等
                     b[h++]=q.front();
            }
        }
        if(!h)
        {
            printf("0\n");
            continue;
        }
        sort(b,b+h);
        printf("%d\n",h);
        int k;
        for(k=0;k<h-1;k++)
            printf("%d ",b[k]);
        printf("%d\n",b[k]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值