Sereja ans Anagrams
Codeforces Round #215 (Div. 1) B:http://codeforces.com/problemset/problem/367/B
题意:给你两个序列a,b,然后给你一个数p,然后让你在a序列中找一个位置q,得以这个位置开始,以后每隔着aq+aq+1*(p)+.......aq+(m-1)*p,这个序列经过重新排序能够等于b,输出,所有的这样的位置。
题解:可以采用递推。先找起点是1,然后再找起点是1+p,找1+p的时候,其实只要在找1的基础上删除最前的那个数,然后再加入一个数就可以了。一次类推,然后,找2开头,2+开头。具体实现的时候,可以看下面代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int N=4e5+;
int a[N],b[N];//记录a,b,序列
int counts[N],ans[N];//counts统计b序列数字出现的次数
int n,m,p,cnt,top;
int main(){
while(~scanf("%d%d%d",&n,&m,&p)){
map<int,int>Qa;//离散化
memset(counts,,sizeof(counts));
memset(a,-,sizeof(a));
memset(b,-,sizeof(b));
cnt=top=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(Qa[a[i]]==)Qa[a[i]]=++cnt;
}
cnt=;bool flag=false;
for(int i=;i<=m;i++){
scanf("%d",&b[i]);
if(Qa[b[i]]==){//如果b序列中数在a中没有出现,就直接不用找了
flag=true;
}
counts[Qa[b[i]]]++;//统计b中数字出现的次数
}
if(!flag){
for(int i=;i<=p;i++){
int ct=,sum0=;//每次查询记录b中数字在a中出现的次数,如果出现m次,说明等于b序列
for(int j=i;j<=n*;j+=p){//注意这里是j<=2*n,虽然在n+1以后的数不会构成b序列,但是这里是为了把之前加入放入数都还原,便于下一次使用
counts[Qa[a[j]]]--;//入队之后次数减一
ct++;
if(counts[Qa[a[j]]]>=)//如果次数在-1的基础上任然大于0,说明,这个数字是b中的数字
sum0++;//注意判断是要求大于0的,因为b的数字可能会重复
if(ct==m){//判断第一次达到m个数是否满足条件
if(sum0==m)
ans[++top]=i;
}
else if(ct>m){//当多余m个时候,就要把最前面的数删除,只保留m个数
if(counts[Qa[a[i+(ct--m)*p]]]>=){//如果这个数次数大于=0,说明这个数被统计过,所以要还原
sum0--;
counts[Qa[a[i+(ct--m)*p]]]++;
}
else{
counts[Qa[a[i+(ct--m)*p]]]++;
}
if(sum0==m){//判断新加入过的数有没有满足条件
ans[++top]=i+(ct-m)*p;
}
}
}
}
}
sort(ans+,ans+top+);
printf("%d\n",top);
for(int i=;i<top;i++)
printf("%d ",ans[i]);
if(top>)
printf("%d\n",ans[top]);
else
printf("\n");
}
}
Sereja ans Anagrams的更多相关文章
- Codeforces Round #215 (Div. 1) B. Sereja ans Anagrams 匹配
B. Sereja ans Anagrams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Sereja ans Anagrams 优先队列控制
Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Si ...
- Codeforces Round #215 (Div. 2) D. Sereja ans Anagrams
http://codeforces.com/contest/368/problem/D 题意:有a.b两个数组,a数组有n个数,b数组有m个数,现在给出一个p,要你找出所有的位置q,使得位置q q+ ...
- cf D. Sereja ans Anagrams
http://codeforces.com/contest/368/problem/D #include <cstdio> #include <cstring> #includ ...
- Codeforces Round #215 (Div. 2) D题(离散化+hash)
D. Sereja ans Anagrams time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 367
A. Sereja and Algorithm 水题不解释. B. Sereja ans Anagrams 模p同余的为一组,随便搞. C. Sereja and the Arrangement of ...
- Codeforces Round #215 (Div. 1)
A Sereja and Algorithm 题意:给定有x,y,z组成的字符串,每次询问某一段s[l, r]能否变成变成zyxzyx的循环体. 分析: 分析每一段x,y,z数目是否满足构成循环体,当 ...
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
随机推荐
- jQuery Animation实现CSS3动画
jQuery Animation的工作原理是通过将元素的CSS样式从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 "margin:3 ...
- SDUTRescue The Princess(数学问题)
题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To re ...
- ios中图片的绘画和截图
ios中图片的绘画和截图 CGImageCreateWithImageInRect截图和UIGraphicsGetImageFromCurrentImageContext绘画图片 使用CGImageC ...
- 在一台电脑访问另一台电脑的mysql数据库
1. 假设192.168.1.3为服务器 2. 首先在ip为192.168.1.103的机子上能够ping 通 运行->cmd >ping 192.168.1.3 检 ...
- [转]Android NDK几点回调方式
一.NDK中获取android设备ID的方式 Java代码如下(获取设备ANDROID_ID): final String androidId = Secure.getString(context.g ...
- Eclipse的修改编码插件使用
最近因为编码问题,很是纠结,终于找到了一个Eclipse的修改编码插件com.lifesting.tool.encoding_1.0.0.jar,使用感觉还不错,记录一下使用方法. 第一步 将插件co ...
- CentOS 6.7编译安装MySQL 5.6
1.安装前准备 yum install make gcc gcc-c++ ncurses-devel perl bison-devel yum groupinstall "Developme ...
- oracle:变长数组varray,嵌套表,集合
创建变长数组类型 ) ); 这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改变长数组类型和嵌套表类型 元素的大小. ALTER TYPE ...
- ecshop了解01
ecshop 是一个基于b2c的开源商城系统,从现在起来我也来学习一下,它是基于面向对象的,当然里面也有类. ecshop 的目录介绍 上面简单介绍一个ecshop的几个主要的文件,上面已经截图给大家 ...
- HBase0.98.1 通过协调器进行表的行数统计
1. 启用表aggregation,只对特定的表生效.通过HBase Shell 来实现 (1)disable指定表.hbase> disable 'student' (2)添加aggregat ...