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 ------------------------------------------------------------------ ...
随机推荐
- android中解析文件的三种方式
android中解析文件的三种方式 好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...
- 史上最简单的带流控功能的http server
s.py import time import SimpleHTTPServer import SocketServer BYTES_PER_SECOND=160*1024 class MyHTTPR ...
- [RxJS] Marble diagrams in ASCII form
There are many operators available, and in order to understand them we need to have a simple way of ...
- [转] 整理了一份React-Native学习指南
自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull requests! React-Native学习指南 本指南汇集React ...
- app 的内存优化
这篇文章是笔者在开发App过程中发现的一些内存问题, 然后学习了YYKit框架时候也发现了图片的缓存处理 (YYKit 作者联系了我, 说明了YYKit重写imageNamed:的目的不是为了内存管理 ...
- Java基础知识强化12:Java中运用数组的四种排序方法
1.使用JavaApi文档中的Arrays类中的sort()进行快速排序 首先我们直接看代码如下: package himi.text; import java.util.Arrays; public ...
- 【转载】Shared Configuration
Introduction The Internet changes the ways in which companies handle their day-to-day business and h ...
- hibernate对象关系映射( 一对一,一对多,多对一,多对多的单向,双向映射 ——)
对象之间的关系: 关系映射之间的关系只的是对象之间的关系,并不指数据库表的关系(外键关系)这儿解决的问题是当对象之间的关系之一时,数据库表该如何映射,编程上如何对待. 一对一(主键关联,和单向的外键关 ...
- Asp.net Mvc4 基于Authorize实现的模块访问权限
在MVC中,我们可以通过在action或者controller上设置Authorize[Role="xxx"] 的方式来设置用户对action的访问权限.显然,这样并不能满足我们的 ...
- Java 6 Thread States and Life Cycle.
Ref: Java 6 Thread States and Life Cycle This is an example of UML protocol state machine diagram sh ...