SAC '22 Code Challenge 5 P5 - Querying Rectangles

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 10.0s
Java 15.0s
Memory limit: 1G

Author:
Problem types

After the evil Max zapped all of Mr. DeMello's flavour text writing ability, Max has decided running out of problems is getting old.

Max started digging out old Yali training camp problems.

Given that Richard Peng is a facilitator of the NA and China problem trade, Max asks him for help.

With Richard Peng's help, he translated one of their problems and nerfed it to two arbitrary values:

Given an N \times M grid of lowercase characters, find the number of distinct subgrids of K \times K.

Since this is a Richard problem, Max cannot solve it, so he asks you for help.

Can you solve it for him?

Constraints

For all subtasks:

1 \le K \le \min(N, M)

Subtask 1 [30%]

1 \le N, M \le 100

Subtask 2 [70%]

1 \le N, M \le 5\,000

Input Specification

The first line will contain three integers, N, M, and K, the number of rows in the grid, the number of columns in the grid, and the size of the subgrid.

The next N lines will contain M space-separated lowercase letters, representing the grid.

Output Specification

Output the number of distinct subgrids.

Sample Input

3 3 2
a b c
d a b
f d a

Sample Output

3

Explanation for Sample Output

There are a total of 4 subgrids of size 2. Out of these grids, only 3 are distinct (the subgrid from (1, 1) to (2, 2) occurs again at (2, 2) to (3, 3)).


Comments

There are no comments at the moment.