Weather
Time limit2sMemory limit512 MB
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.
Problem
Predicting the weather for the next days would be interesting. Let , , , stand for 26 different weather types. Write the weather of the next days as , where each belongs to .
Meteorologists made a breakthrough and built a machine that reports weather patterns, where one weather pattern is the weather types of consecutive days. Suppose , , 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 weather patterns in alphabetical order: CCC, CCR, CRS, CRS, RSC, RSR, SCC, SRR. The same weather pattern can appear more than once.
and 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 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 and separated by a space (, , ).
Each of the next lines contains one weather pattern, listed in alphabetical order. Every weather pattern is uppercase letters.
Output
Print two characters, the weather type of day 1 and the weather type of day , 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 in which every weather pattern is an edge between the node and the node . Reconstructing the weather sequence is the same as finding a path that covers every edge of .