Hash练习题:【213E】 Two Permutations(线段树,hash)

来源:互联网 发布:安信通达信软件 编辑:程序博客网 时间:2024/06/01 10:34

文章出处: http://blog.csdn.net/ACM_cxlove?viewmode=contents


原题:http://codeforces.com/problemset/problem/213/E


尼玛,这是研究题解都研究了半天,巨弱啊

开始看成是连续的子串,然后就直接HASH枚举了一下,果断跪。

然后就想到可以不连续,然后就没啥想法了。

最终还是用线段树去维护一个HASH

按数字顺序,依次插入线段树,然后对整个区间求HASH。其实就是比较的是整个区间的相对顺序是否 和A串一致。

比如说A串中是1 3 2

那么将第二个串的1 2 3插入到线段树中后,如果其相对顺序也是1 3 2 那么HASH值肯定一样。

但是注意的是,如果继续插入4,肯定要把1删掉,那么这时候的HASH值是2,3,4的组合HASH。

可以发现每一权都加了1,那么这个预处理一下就可以了

HASH我还是习惯性地两次HASH,减少错误的可能性

E. Two Permutations
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Rubik is very keen on number permutations.

permutation a with length n is a sequence, consisting of n different numbers from 1 to n. Element number i (1 ≤ i ≤ n) of this permutation will be denoted as ai.

Furik decided to make a present to Rubik and came up with a new problem on permutations. Furik tells Rubik two number permutations: permutation a with length n and permutation b with length m. Rubik must give an answer to the problem: how many distinct integers dexist, such that sequence c (c1 = a1 + d, c2 = a2 + d, ..., cn = an + d) of length n is a subsequence of b.

Sequence a is a subsequence of sequence b, if there are such indices i1, i2, ..., in (1 ≤ i1 < i2 < ... < in ≤ m), that a1 = bi1a2 = bi2...,an = bin, where n is the length of sequence a, and m is the length of sequence b.

You are given permutations a and b, help Rubik solve the given problem.

Input

The first line contains two integers n and m (1 ≤ n ≤ m ≤ 200000) — the sizes of the given permutations. The second line contains ndistinct integers — permutation a, the third line contains m distinct integers — permutation b. Numbers on the lines are separated by spaces.

Output

On a single line print the answer to the problem.

Sample test(s)
input
1 111
output
1
input
1 212 1
output
2
input
3 32 3 11 2 3
output
0



0 0