CF BUDDY
← Problems·

1955D · Inaccurate Subsequence Search

1400 · data structures, two pointers

Problem: Maxim has an array aa of nn integers and an array bb of mm integers (mnm \le n).

Maxim considers an array cc of length mm to be good if the elements of array cc can be rearranged in such a way that at least kk of them match the elements of array bb.

For example, if b=[1,2,3,4]b = [1, 2, 3, 4] and k=3k = 3, then the arrays [4,1,2,3][4, 1, 2, 3] and [2,3,4,5][2, 3, 4, 5] are good (they can be reordered as follows: [1,2,3,4][1, 2, 3, 4] and [5,2,3,4][5, 2, 3, 4]), while the arrays [3,4,5,6][3, 4, 5, 6] and [3,4,3,4][3, 4, 3, 4] are not good.

Maxim wants to choose every subsegment of array aa of length mm as the elements of array cc. Help Maxim count how many selected arrays will be good.

In other words, find the number of positions 1lnm+11 \le l \le n - m + 1 such that the elements al,al+1,,al+m1a_l, a_{l+1}, \dots, a_{l + m - 1} form a good array.

Input Format: The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains three integers nn, mm, and kk (1kmn21051 \le k \le m \le n \le 2 \cdot 10^5) — the number of elements in arrays aa and bb, the required number of matching elements.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1061 \le a_i \le 10^6) — the elements of array aa. Elements of the array aa are not necessarily unique.

The third line of each test case contains mm integers b1,b2,,bmb_1, b_2, \dots, b_m (1bi1061 \le b_i \le 10^6) — the elements of array bb. Elements of the array bb are not necessarily unique.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5. Similarly, it is guaranteed that the sum of mm over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output the number of good subsegments of array aa on a separate line.

Note: In the first example, all subsegments are good.

In the second example, good subsegments start at positions 11, 22, and 33.

In the third example, good subsegments start at positions 11 and 22.

Sample Cases

Case 1

Input

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

Output

4
3
2
4
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback