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的更多相关文章

  1. 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 ...

  2. Sereja ans Anagrams 优先队列控制

    Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Si ...

  3. 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+ ...

  4. cf D. Sereja ans Anagrams

    http://codeforces.com/contest/368/problem/D #include <cstdio> #include <cstring> #includ ...

  5. 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 ...

  6. Codeforces 367

    A. Sereja and Algorithm 水题不解释. B. Sereja ans Anagrams 模p同余的为一组,随便搞. C. Sereja and the Arrangement of ...

  7. Codeforces Round #215 (Div. 1)

    A Sereja and Algorithm 题意:给定有x,y,z组成的字符串,每次询问某一段s[l, r]能否变成变成zyxzyx的循环体. 分析: 分析每一段x,y,z数目是否满足构成循环体,当 ...

  8. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. LeetCode - 49. Group Anagrams

    49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...

随机推荐

  1. cocos2d&amp;cocos2dx学习资源

    汇总一下自己学习Cocos2d和cocos2dx认为比較好的一些资源: 书籍: <iPhone&iPad cocos2d游戏开发实战> Steffen Itterheim < ...

  2. Linux内核中SPI/I2c子系统剖析

    Linux内核中,SPI和I2C两个子系统的软件架构是一致的,且Linux内核的驱动模型都以bus,driver,device三种抽象对象为基本元素构建起来.下文的分析将主要用这三种抽象对象的创建过程 ...

  3. Windows、Ubuntu双系统重装windows系统后grub引导的修复及默认启动项的修改

    今天帮童鞋重装系统,他的电脑是Windows.Ubuntu双系统,需要重装的系统是windows,据说是因为很多游戏都只支持64位,要给换成64位的 = =...      于是我就帮他装了个wind ...

  4. android 43 SQLite数据库

    SQLite数据库很小,占用内存只有几百K,安卓和IOS都是用的SQLite数据库. 页面: <LinearLayout xmlns:android="http://schemas.a ...

  5. 使用 asp.net mv4开发企业级办公OA

    大家好!这是我第一次写asp.net 开发笔记,哪里写的不好,请见谅! 本程序是一个在线办公(OA)系统 B/S项目: 项目开发环境:Microsoft Visual Studio 2012 + Sq ...

  6. Writing Your First Test

    Let's say you have an activity layout that represents a welcome screen: <?xml version="1.0&q ...

  7. HTML - 键盘事件

    Keyboard 事件 onkeydown: 在用户按下按键时触发. onkeypress: 在用户敲击按钮时触发. onkeyup: 当用户释放按键时触发. 示例 <!DOCTYPE html ...

  8. android 简单的开机自启

    今天我们主要来探讨android怎么让一个service开机自动启动功能的实现.Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.in ...

  9. Scrum教练不应兼任product owner

    ScrumMasters Should Not Also Be Product Owners(中文翻译) December 2, 2014 by Mike Cohn 翻译:2015.2.18 by o ...

  10. MySQL数据库迁移详细步骤(转)

    ========================================================================================== 一.背景简介 == ...