problems / 210

Suki's Tile Pattern

easy matrix simulation 1000 ms · 256 MB

Suki's Tile Pattern

Suki is redecorating her garden and wants to create a beautiful tiled patio using a repeating pattern. She has a small tile pattern (a square template) that she wants to repeat across a rectangular garden plot.

Given a repeating tile pattern of size p × p and dimensions n × m for her garden plot, help Suki determine what the final tiled garden will look like.

The pattern repeats both horizontally and vertically. Each cell (i, j) in the garden at row i and column j (0-indexed) will display the tile from position (i mod p, j mod p) in the pattern.

Input

The first line contains three integers n, m, and p (1 ≤ n, m ≤ 100, 1 ≤ p ≤ 10).

  • n is the number of rows in the garden plot
  • m is the number of columns in the garden plot
  • p is the size of the repeating pattern (a p × p square)

The next p lines each contain p characters representing the tile pattern. Each character represents a different tile type.

Output

Print the final tiled garden as n lines, each with m characters, showing how the pattern repeats across the entire garden plot.

Notes

The pattern repeats infinitely in both directions. The garden plot is positioned starting at (0, 0) of this infinite repeating pattern, so position (i, j) in the garden shows the tile from position (i mod p, j mod p) in the pattern.

Example 1

Input

4 4 2
AB
CD

Output

ABAB
CDCD
ABAB
CDCD

Example 2

Input

2 2 3
123
456
789

Output

12
45

Hints · reveal one at a time if you're stuck

Hint 1

The pattern repeats in both directions with period p. Does the tile at a garden cell depend on anything other than its row and column, and how does the row index relate to the pattern's rows?

Hint 2

Use the remainder of each row and column index when divided by p to look up the tile in the pattern. Build each of the n output lines as a string of length m.

report an issue Prefer your own editor? sooks prep 210 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.