B. Sereja ans Anagrams

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/367/B

Description

Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers 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 ≤ n; q ≥ 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 of the input data contains the single integer n (1 ≤ n ≤ 50) — the number of commands.

Then
follow n lines, each contains one command. Each of these lines contains
either command pwd, or command cd, followed by a space-separated
non-empty parameter.

The command parameter cd only contains lower
case Latin letters, slashes and dots, two slashes cannot go
consecutively, dots occur only as the name of a parent pseudo-directory.
The command parameter cd does not end with a slash, except when it is
the only symbol that points to the root directory. The command parameter
has a length from 1 to 200 characters, inclusive.

Directories in the file system can have the same names.

Output

The first line contains three integers n, m and p (1 ≤ n, m ≤ 2·105, 1 ≤ p ≤ 2·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 109).

Sample Input

5 3 1
1 2 3 2 1
1 2 3

Sample Output

2
1 3

HINT

题意

给你n个a[i],m个b[i],和P

要求让你找到起点,使得a[pos],a[pos+p],a[pos+2*p]……,a[pos+(m-1)*p]和b数组一样

题解:

类似于km算法

我们把b[i]表示为子串

a[i]表示为母串

因为不要求顺序,我们就直接跑就好了,只要匹配不是O(m*n)的都能过吧

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1050005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int a[maxn];
int b[maxn];
map<int,int> H;
map<int,int> H1;
vector<int> ans;
int main()
{
int n=read(),m=read(),p=read();
for(int i=;i<=n;i++)
a[i]=read();
for(int i=;i<=m;i++)
b[i]=read(),H[b[i]]++;
int pos;
int flag=;
for(int i=;i<=p;i++)
{
H1.clear(),pos=i,flag=;;
for(int j=i;j<=n;j+=p)
{
H1[a[j]]++,flag++;;
if(H1[a[j]]>H[a[j]])
{
for(int k=pos;k<=j;k+=p)
{
H1[a[k]]--,flag--;
if(a[j]==a[k])
{
pos=k+p;
break;
}
}
continue;
}
if(flag==m)
{
ans.push_back(pos);
H1[a[pos]]--;
flag--;
pos+=p;
}
}
}
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(int i=;i<ans.size();i++)
printf("%d ",ans[i]); }

Codeforces Round #215 (Div. 1) B. Sereja ans Anagrams 匹配的更多相关文章

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

  2. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes map

    B. Sereja and Suffixes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  3. Codeforces Round #215 (Div. 2) C. Sereja and Algorithm

    #include <iostream> #include <vector> #include <algorithm> #include <string> ...

  4. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes

    #include <iostream> #include <vector> #include <algorithm> #include <set> us ...

  5. Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

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

  7. Codeforces Round #215 (Div. 1)

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

  8. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  9. Codeforces Round #223 (Div. 2)--A. Sereja and Dima

    Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. Web表格

    HTML元素学习 1:表格:表格的作用是显示表格数据,小范围内布局 表格的框架 <!doctype html> <html lang="en"> <h ...

  2. mybatis源码学习: 动态代理的应用(慢慢改)

    动态代理概述 在学spring的时候知道使用动态代理实现aop,入门的列子:需要计算所有方法的调用时间.可以每个方法开始和结束都获取当前时间咋办呢.类似这样: long current=system. ...

  3. arp spoofing

    Today our tutorial will talk about Kali Linux Man in the Middle Attack. How to perform man in the mi ...

  4. [转]Javascript定义类的三种方法

    作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html 将近2 ...

  5. SSO单点登录在web上的关键点 cookie跨域

    概述 其实WEB单点登录的原理挺简单的,抛开那些复杂的概念,简单来讲讲如何实现一个最基本的单点登录 首先需要有两个程序 例如:http://www.site-a.com 我们简称A http://ww ...

  6. docker集成管理工具-shipyard的开发环境搭建笔记

    前段时间一直在研究openstack,后来老师告诉我需要用docker容器来搭建hadoop集群,所以就将战场转移到docker上来了,话说docker最近这段时间太火了,但是说实话我觉得应用起来还不 ...

  7. MySQL [Warning] Can’t create test file xxx lower-test(转)

    add by zhj:修改的数据库的datadir,然后数据库就无法启动了,错误如下 2014-12-11 16:22:57 26309 [Warning] Can't create test fil ...

  8. VMare中安装“功能增强工具”,实现CentOS5.5与win7host共享文件夹的创建

    读者如要转载,请标明出处和作者名,谢谢. 地址01:http://space.itpub.net/25851087 地址02:http://www.cnblogs.com/zjrodger/ 地址03 ...

  9. HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))

    http://acm.hdu.edu.cn/showproblem.php?pid=5423 题目大意:给你一个树 判断这棵树是否是独特的 一颗树是独特的条件:不存在一颗和它本身不同但相似的树 两颗树 ...

  10. QListWidgetItem带上颜色的问题

    new_item = QListWidgetItem(_fromUtf8(item_content), self.listWidget) 首先创建一个QListWidgetItem,第一个参数内容是I ...