D. Sereja ans Anagrams
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ nq ≥ 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 nm and p (1 ≤ n, m ≤ 2·105, 1 ≤ p ≤ 2·105). The next line contains n integers a1a2, ..., an (1 ≤ ai ≤ 109). The next line contains m integers b1b2, ..., 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.

Sample test(s)
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

题意:很容易理解。

分析:离散化->用hash进行线性求解

比赛的时候没有做出来,后来看了别人的思路,然后自己敲了遍代码,贡献了一次超时和两次Runtime Error,不过从中也得到一些收获,题目中a数组和b数组的元素范围为1<=ai,bi<=10^9,此范围比较大,开数组进行hash不好弄,所以第一步就是离散化,离散化之后就是hash进行求解,实践复杂度为线性的,具体看代码实现。

代码实现:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int n,m,p,total,flag,now;
int a[],b[],c[],all[],visited[]; void hebing()//合并成一个数组,全部存在c数组里
{
int i,j,x;
total=;
for(i=;i<=n;i++)
c[++total]=a[i];
for(i=;i<=m;i++)
c[++total]=b[i];
sort(c+,c++total);
x=;
for(i=;i<=total;i++)//去重
if(i==||c[i]!=c[i-])
c[++x]=c[i];
total=x;
} int find(int x)//二分查找x处在c数组中的位置
{
int l=,r=total+,mid;
while(l<=r)
{
mid=(l+r)>>;
if(c[mid]==x)
return mid;
else if(c[mid]>x)
r=mid-;
else
l=mid+;
}
} void lisanhua()//对a数组和b数组进行离散化
{
int i;
for(i=;i<=n;i++)
a[i]=find(a[i]);
for(i=;i<=m;i++)
b[i]=find(b[i]);
} void init()
{
int i;
flag=;
memset(all,,sizeof(all));
for(i=;i<=m;i++)//哈希处理b数组的情况
{
if(all[b[i]]==)
flag++;
all[b[i]]++;
}
} void add(int x)
{
visited[x]++;
if(visited[x]==all[x])
now++;
} void del(int x)
{
visited[x]--;
if(visited[x]+==all[x])
now--;
} void solve()
{
int res[],num=,i,j,k;
for(i=;i<=p;i++)//因为相隔为p,所以只需枚举1-p,然后对每一种序列滚动过去
{
if((i+(long long)(m-)*p)<=n)//因为(m-1)*p已经超过int型,所以要强制转换成long long
{ //否则就会出现Runtime Error
now=;
for(j=;j<m;j++)
add(a[i+j*p]);
if(now==flag)
res[num++]=i;
for(j=i+p;(j+(long long)(m-)*p)<=n;j=j+p)//滚动过去
{
del(a[j-p]);
add(a[j+(m-)*p]);
if(now==flag)
res[num++]=j;
}
for(k=;k<m;k++)//把加了的删掉,开始我用了memset(visited,0,sizeof(visited));
del(a[j-p+k*p]);//然后果断超时了
}
}
printf("%d\n",num);
sort(res,res+num);
for(i=;i<num;i++)
if(i!=num-)
printf("%d ",res[i]);
else
printf("%d\n",res[i]);
} int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&p);
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=m;i++)
scanf("%d",&b[i]);
hebing();
lisanhua();
init();
solve();
}
return ;
}

Codeforces Round #215 (Div. 2) D题(离散化+hash)的更多相关文章

  1. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  2. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  3. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  4. Codeforces Round #552 (Div. 3) A题

    题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...

  5. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. Codeforces Round #361 (Div. 2) 套题

    A - Mike and Cellphone 问有没有多解,每个点按照给出的序列用向量法跑一遍 #include<cstdio> #include<cstring> #incl ...

  7. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  8. Codeforces Round #425 (Div. 2))——A题&&B题&&D题

    A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...

  9. Codeforces Round #579 (Div. 3) 套题 题解

    A. Circle of Students      题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...

随机推荐

  1. 2011 ACM/ICPC 成都赛区(为2013/10/20成都现场赛Fighting)

    hdu 4111  Alice and Bob 博弈:http://www.cnblogs.com/XDJjy/p/3350014.html hdu 4112 Break the Chocolate ...

  2. 重温《js权威指南》 第2-3章

    第二章 语法结构         2.1 js区分大小写,html不区分大小写         2.5 注意分号,如果没有分号,解释器会试图解析js,并在不能解析的地方加分号 第三章 值和变量     ...

  3. linux下ping加时间戳实时输出到文件 放后台运行

    放后台运行命令:setsid 实时输出命令:unbuffer 加时间戳:awk '{ print $0"\t" strftime("%D_%H:%M:%S",s ...

  4. JavaWeb项目开发案例精粹-第6章报价管理系统-001需求分析及设计

    1. 2. 3. 4. 5. 6.

  5. iOS:核心动画之动画组CAAnimationGroup

    CAAnimationGroup——动画组 动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行 属性说明: ...

  6. Wordnet 与 Hownet 比较

    近年来,随着计算机本身以及信息高速公路的飞速发展,人们开始更加重视语义的研究.各国都致力于可用于自然语言处理的大规模语义词典或大规模知识库的建设.例如:普林斯顿大学的英语Wordnet,微软的Mind ...

  7. Java 数据结构之vector

    Vector 实现了一个动态数组.是可实现自动增长的对象数组. vector和arraylist的比较: 1.都是采用数组格式存储数据,索引数据块插入数据慢 2.ArrayList会比Vector快, ...

  8. 设置Windows Azure Linux虚拟机中的root账户

    使用Windows Azure 创建好Linux虚拟机之后,如果你使用默认的用户密码登陆root是不行的,如下图所示: 其原因是Windows Azure创建Linux虚拟机时并没有同时设置root密 ...

  9. 理解Java的封装与接口

    1.封装,即保留有限的外部接口(interface),隐藏具体实施细节. 2.封装在生活中很常见.比如下面是一个充电电筒: 一个用户即使不看说明书,也可以猜到这个电筒的操作: 开关和充电.这个电筒用一 ...

  10. hibernate基础(1)

    hibernate基础1.hibernate介绍与动手入门体验  问题:模型不匹配(java对象模型与数据库关系模型不匹配)  解决: 1.使用JDBC手工转换        2.使用ORM(Obje ...