Back to Space
Time limit2sMemory limit512 MB
Given an r by c grid of colored tiles, find a panel size a by b and a row shift s so that the grid is covered by shifted copies of the panel, with minimal panel area.
- Level
Hard9 of 10
- Topics
- String matching, Hash map, Math
- Solved
- No attempts yet
Statement
For the 50th anniversary of the first crewed spaceflight, a new reusable spacecraft called "Vostorg" is being built. The rectangular part of its hull (called the rectangle below) must be covered with square heat-shield tiles of one size and several colors. The rectangle has rows of tiles each. The tiles must form a given pattern.
Laying tiles one at a time is too laborious, so the pattern is built from identical rectangular panels made of tiles. Panels are attached one after another, filling the rectangle row by row from top to bottom. Each row of panels may be shifted to the right by the same number of tiles relative to the previous row. Panels may extend beyond the rectangle. All panels must have the same orientation: when one panel is translated onto another, the colors of their tiles must match.
The chief designer wants to pick a panel size and a shift so that the pattern can be laid out with these panels, and the panel area is as small as possible. Here is the shift to the right of each next row relative to the previous one.

An example of panels with , , .
Input
The first line contains two integers and , the size of the rectangle in tiles. The next lines give the tile colors. Each of the colors is one of the first uppercase Latin letters. It is guaranteed that a panel of size exists for this rectangle with and .
Output
Print three integers , , and that satisfy the conditions. If several solutions exist, print any of them.
Limits
The shift has no restriction. Both and do not exceed 1961.
Hint
The tiling for the second example is shown below. Parts of the panels that stick out past the rectangle are not drawn.
