#K77142. Most Common Tree Species
Most Common Tree Species
Most Common Tree Species
You are given a grid consisting of tree species. Your task is to determine the most common tree species in the grid and the number of times it occurs. In case of a tie (i.e. more than one species has the same maximum frequency), output the species that comes first in lexicographical order.
The problem can be mathematically described as follows:
Let \(G\) be a set of species strings contained in a grid of size \(R \times C\). Define a function \(f(s)\) as the frequency of species \(s\) in \(G\). Find \(s^*\) such that:
[ s^* = \min{ s \mid f(s)=\max_{t \in G} f(t) }]
and output \(s^*\) along with \(f(s^*)\).
inputFormat
The first line contains two integers \(R\) and \(C\) separated by a space, denoting the number of rows and columns of the grid, respectively.
Each of the next \(R\) lines contains \(C\) space-separated strings representing the tree species in that row.
outputFormat
Output a single line containing the most common tree species and its frequency, separated by a space.
## sample3 3
oak pine birch
oak birch pine
pine oak oak
oak 4