This page is still under construction.

Parts of this page are still being built. What you see may change.

Weather

Time limit2sMemory limit512 MB

Summary
Reconstruct the first and last days of a weather string from its multiset of length-d substrings, choosing the alphabetically smallest pair when several fit.
Level

Medium7 of 10

Topics
Graph, String
Solved
No attempts yet

Problem

Predicting the weather for the next NN days would be interesting. Let AA, BB, …\ldots, ZZ stand for 26 different weather types. Write the weather of the next NN days as W[1],…,W[N]W[1], \ldots, W[N], where each W[i]W[i] belongs to {A,B,…,Z}\{A, B, \ldots, Z\}.

Meteorologists made a breakthrough and built a machine that reports N−d+1N - d + 1 weather patterns, where one weather pattern is the weather types of dd consecutive days. Suppose N=10N = 10, d=3d = 3, and the weather of the ten days is CRSCCCRSRR. Here S is a sunny day, C is a cloudy day, and R is a rainy day. The machine then reports N−d+1=8N - d + 1 = 8 weather patterns in alphabetical order: CCC, CCR, CRS, CRS, RSC, RSR, SCC, SRR. The same weather pattern can appear more than once.

NN and dd are fixed, and you are given the list of weather patterns the machine produced. Compute the weather type of day 1 and the weather type of day NN so that both agree with the reported patterns. For the example above the answer is C and R.

Input

The first line contains two integers NN and dd separated by a space (3≤N≤10003 \le N \le 1000, 3≤d≤203 \le d \le 20, d≤Nd \le N).

Each of the next N−d+1N - d + 1 lines contains one weather pattern, listed in alphabetical order. Every weather pattern is dd uppercase letters.

Output

Print two characters, the weather type of day 1 and the weather type of day NN, with no space between them.

Several answers can agree with the given list of weather patterns. In that case print the answer whose two characters, read as a string, come first in alphabetical order.

Hint

The problem can be restated as a graph problem. Build a graph GG in which every weather pattern P[1]P[2]…P[d]P[1] P[2] \ldots P[d] is an edge between the node P[1]…P[d−1]P[1] \ldots P[d-1] and the node P[2]…P[d]P[2] \ldots P[d]. Reconstructing the weather sequence is the same as finding a path that covers every edge of GG.

Examples1

  1. Example 1

    Input
    10 3
    CCC
    CCR
    CRS
    CRS
    RSC
    RSR
    SCC
    SRR
    
    Expected output
    CR