博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #215 (Div. 1) B. Sereja ans Anagrams 匹配
阅读量:5258 次
发布时间:2019-06-14

本文共 3129 字,大约阅读时间需要 10 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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-9const int inf=0x3f3f3f3f;const ll infll = 0x3f3f3f3f3f3f3f3fLL;inline ll read(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}//**************************************************************************************int a[maxn];int b[maxn];map
H;map
H1;vector
ans;int main(){ int n=read(),m=read(),p=read(); for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=m;i++) b[i]=read(),H[b[i]]++; int pos; int flag=0; for(int i=1;i<=p;i++) { H1.clear(),pos=i,flag=0;; 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<
<

 

转载于:https://www.cnblogs.com/qscqesze/p/4618771.html

你可能感兴趣的文章
BootStrap学习
查看>>
爬虫学习-入门
查看>>
mongodb副本集部署
查看>>
celery学习笔记
查看>>
django rest framework自定义返回格式
查看>>
设置mysql的远程访问权限
查看>>
eclipse查找多种类型文件中的关键字
查看>>
eclipse老是自动跳转控制台
查看>>
plsql弹出注册框
查看>>
Marshmallow详解
查看>>
TrueCrypt
查看>>
Android Studio|IntelliJ IDEA 常用快捷键(Mac|Window)
查看>>
tbl.js div实现的表格控件,完全免费,no jquery
查看>>
WIN32得到HWND
查看>>
一段时间Mysql弹出Start Initialization installer
查看>>
《人类简史》十三、不断追寻(下)——重新认识自己
查看>>
POJ 2075
查看>>
php redis
查看>>
c# 截屏
查看>>
第二次Java作业--计科1501李俊
查看>>