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. SPF详解2

    什么是SPF?   这里的SPF不是防晒指数,而是指Sender Policy Framework.翻译过来就是发信者策略架构,比较拗口,通常都直接称为SPF.   SPF是跟DNS相关的一项技术,它 ...

  2. hdu 5254 水题

    纯暴力就能过的,可是题目描述真心不清楚,我看了好久好久才明白题目啥意思. 为了迅速打完,代码比较冗余. /* * Author : ben */ #include <cstdio> #in ...

  3. 我常用的VBS方法(QTP)

    这些是4年前在HP用QTP做自动化测试时候总结的一些,现在贴出来,说不准以后会不会用到 当初花了2天时间写的一个自动生成的Excel Report Public Function Report (st ...

  4. oracle导入导出数据库和创建表空间和用户

    直入主题: 首先在本地创建2个文件,D:\oradata\jgszz\temp.dbf和 D:\oradata\jgszz\data.dbf. 然后执行下面的SQL. /*创建临时表空间 */ cre ...

  5. python27+django1.9创建app的视图及实现动态页面

    一.简易静态视图 views文件里写: from django.http import HttpResponse def hello(request): return HttpResponse(&qu ...

  6. 序列for循环语句

    序列for循环语句 序列for循环语句允许重复遍历一组序列,而这组序列可以是任何可以重复遍历的序列,如由begin()和end()函数定义的STL序列.所有的标准容器都可用作这种序列,同时它也同样可以 ...

  7. 镜头覆盖范围FOV 的规划与FOV去四角

    本文纲要: 规划FOV的原因 FOV去四角 简单的声明 FOV查找思路 查找FOV的代码 TNoFourCornersRect结构及Contains函数 判断两个矩形是否相交 延伸与拓展 本文内容: ...

  8. N皇后问题--回溯法

    1.引子 中国有一句古话,叫做“不撞南墙不回头",生动的说明了一个人的固执,有点贬义,但是在软件编程中,这种思路确是一种解决问题最简单的算法,它通过一种类似于蛮干的思路,一步一步地往前走,每 ...

  9. C#获取ftp文件最后修改时间

    public static DateTime GetFileModifyDateTime(string ftpServerIP,string ftpFolder,string ftpUserID,st ...

  10. Microsoft Azure云计算第一步—试用帐户申请

    从本文开始,将会对Microsoft Azure云从Iaas, Paas, Saas三种类型的云应用通过文章进行介绍.千里之行,始于帐户:),如果大家需要申请免费试用帐户请参考本文. 对于直接付钱的壕 ...