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 ------------------------------------------------------------------ ...
随机推荐
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试 系列目录 我想测试EF在一百万条数据下的显示时间! ...
- Mean Shift具体介绍
Mean Shift,我们 翻译为“均值飘移”.其在聚类,图像平滑.图像切割和跟踪方面得到了比較广泛的应用.因为本人眼下研究跟踪方面的东西,故此主要介绍利用Mean Shift方法进行目标跟踪,从而对 ...
- Codeforces 15B Laser
题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...
- Python异常处理 分类: python Raspberry Pi 服务器搭建 2015-04-01 13:22 172人阅读 评论(0) 收藏
一个程序要保持稳定运行必须要有异常处理,本文将简单介绍Python中的try-except..异常处理语句的使用. 该种异常处理语法的规则是: 执行try下的语句,如果引发异常,则执行过程会跳到第一个 ...
- DECIMAL Data Type
In MySQL, DECIMAL(M,D) and NUMERIC(M,D) are the same, and both have a precision of exactly M digits. ...
- python-gdb
https://blog.log4d.com/2013/11/python-gdb/ https://wiki.python.org/moin/DebuggingWithGdb
- syslog实例详解rsyslog
http://blog.csdn.net/chenhao112358/article/details/40892239http://www.cnblogs.com/blueswu/p/3564763. ...
- windows10UWP:如何在xaml中设置控件为 public ?
windows10UWP中,由于使用页面导航,操作在不同一个页面的控件需求经常遇到. 如果要对另一个page里面的控件进行操作,那么这个控件必须设置为 public .在 xaml 设置控件的方法是: ...
- 关于echarts绘图,主题的更换
echarts主题进行更换步骤: 模块化单文件引入(推荐) 1.查看自己想要的主题 echarts官网 http://echarts.baidu.com/echarts2/doc/example/t ...
- idea使用笔记
常用快捷键 ctrl+shift+f12 编辑器全屏 win8下输入法不跟随 使用微软输入法即可 默认设置 之前创建maven工程 每次都要选择自己的版本,原来有个默认全局设置 创建maven模板工程 ...