luogu#P4696. [CEOI 2011] Matching

    ID: 3685 远端评测题 2000ms 63MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2011线段树离散化CEOI(中欧)哈希 hashingKMP 算法链表

[CEOI 2011] Matching

Problem Description

For an integer sequence (a1,a2,,an)(a_1,a_2,\cdots,a_n) and a permutation (p1,p2,,pn)(p_1,p_2,\cdots,p_n) of 1n1 \sim n, we say that (a1,a2,,an)(a_1,a_2,\cdots,a_n) matches (p1,p2,,pn)(p_1,p_2,\cdots,p_n) if and only if:

  • Any two numbers in {a}\{a\} are different from each other.
  • After sorting (a1,a2,,an)(a_1,a_2,\cdots,a_n) in increasing order, we obtain (ap1,ap2,,apn)(a_{p_1},a_{p_2},\cdots,a_{p_n}).

Now you are given a permutation {p}\{p\} of 1n1 \sim n and a sequence h1,h2,,hmh_1,h_2,\cdots,h_m. Please determine which substrings of {h}\{h\} match the permutation {p}\{p\}.

Input Format

The first line contains two positive integers n,mn,m separated by spaces.

The second line contains nn positive integers separated by spaces, representing the permutation pp.

The third line contains mm positive integers separated by spaces, representing the sequence hh.

Output Format

The first line contains an integer kk, indicating the number of substrings that match {p}\{p\}.

The second line contains kk positive integers separated by spaces, indicating the starting positions of these substrings (indexed from 11). Output these positions in increasing order. In particular, if k=0k=0, you should output an empty line as well.

5 10
2 1 5 3 4
5 6 3 8 12 7 1 10 11 9
2
2 6

Hint

For 100%100\% of the testdata, $2\le n\le m\le 1\ 000\ 000;1\le h_i\le 10^9;1\le p_i\le n$. It is guaranteed that all elements in {h}\{h\} are pairwise distinct, and {p}\{p\} is a permutation.

Constraints

Translated by ChatGPT 5