Water?
Time limit1sMemory limit128 MB
Given an h by w grid of letters and a set of special letters, find the subrectangle with both sides at least m whose fraction of special pixels is maximum, breaking ties by larger area.
- Level
Medium7 of 10
- Topics
- Prefix sum, Brute force, Math, Implementation
- Solved
- No attempts yet
Problem
One of the main goals of sending the Curiosity rover to Mars is to look for more evidence that Mars may have had water on its surface in the past, or perhaps still does today. To decide whether water was present, scientists rely on clues in the images, such as rust and other signs of oxidation. In this problem you examine pictures of the Martian surface and find evidence of water in them if it exists.
Each picture is given as a rectangle of pixels, with . Every pixel indicates a type of material, written as one upper-case letter from A to Z (no other characters appear). Some of these letters are marked as special: they are the clues for water. Given a minimum size with , find the largest density of special letters over all axis-aligned subrectangles whose height and width are both at least .
The density of a rectangle is the number of special pixels it contains divided by its total number of pixels.
Input
The first line contains the number of data sets. Each of the data sets has the following form:
- One line with three integers , , : the image height, the image width, and the minimum rectangle size.
- One line with a string of to distinct upper-case letters (
A–Z, not necessarily sorted). These are the special, water-indicating letters. - lines follow, each consisting of exactly upper-case letters describing one row of the image.
Output
For each data set, print Data Set x: on its own line, where is the data set number (starting from ). On the next line print the maximum density as a fraction a/b, where is the number of special pixels and is the total number of pixels of the chosen rectangle, considering only rectangles whose height and width are both at least .
The fraction is written exactly as counted and is not reduced (for example 8/16, not 1/2). If several rectangles reach the same maximum density, choose the one with the largest total number of pixels ; this makes the answer unique.
Separate consecutive data sets with a blank line.