Cipher
Time limit1sMemory limit128 MB
Find the a x b subarray that occurs exactly k times (k >= 3) in an n x m character grid and list all its top-left positions in row-major order.
- Level
Hard8 of 10
- Topics
- Hash map, String, Implementation, Sorting
- Solved
- No attempts yet
Problem
While exploring the Universe, a space agency found traces of extraterrestrial (ET) intelligence: a collection of rectangular metal plates carrying messages written in an alien language.
Each plate holds a 2D array with rows and columns. Every cell is a printable ASCII character whose numeric code lies in the range . Each plate also carries two integers, and .
The researchers concluded that a message can be decrypted with a cipher: a key, hidden inside the plate's array, that reveals how to read the message. The cipher is the rectangular subarray of the message that occurs exactly times, with . Occurrences of the cipher may overlap. It is guaranteed that no other subarray occurs more than times, so the cipher is unique.
For example, suppose the array is (, ), the cipher size is (, ), and the cipher occurs times (). Then no other subarray of this array occurs more than times.
Given the array that represents the message and the integers and written on the plate, find the cipher and all positions on the plate where it occurs.
Input
The first line contains two integers and , separated by a space. Each of the next lines contains a string of exactly characters; the -th of these lines is row of the array. The last line contains the two integers and , separated by a space.
Output
On the first line print the integers and , separated by a space (exactly the values from the input). Then print lines, each a string of characters, giving the cipher. On the next line print the integer , the number of times the cipher occurs in the array. Then print lines, each containing two integers: the row and the column (1-indexed) of the upper-left corner of one occurrence of the cipher. Print these pairs sorted in increasing order of row, breaking ties by increasing column.
Constraints
- Rows and columns are numbered from , starting at the upper-left corner.
Hint
An example array, with the four occurrences of its cipher highlighted, is shown below:
