#C1528. Lexicographically Sorted Grid
Lexicographically Sorted Grid
Lexicographically Sorted Grid
You are given an grid of lowercase letters. Your task is to determine whether it is possible to rearrange the rows of the grid so that the letters in every column become sorted in non-decreasing order. In other words, after rearranging, for every column (with ) and for every row (with ), the following condition must hold: [ \text{grid}[i][j] \leq \text{grid}[i+1][j] ]
For example, consider the following grid with , :
cba fgh ijk
If we rearrange the rows (if needed) so that the grid becomes:
cba fgh ijk
Then for each column the characters are in non-decreasing order (, , ), and the answer is possible
.
However, if no such rearrangement exists that will yield sorted columns, then the answer must be impossible
.
inputFormat
The input is given via standard input and has the following format:
The first line contains two integers and , denoting the number of rows and columns respectively. Each of the next lines contains a string of length , representing one row of the grid.
You may assume that the grid consists of lowercase English letters.
outputFormat
Output a single line to standard output containing either possible
or impossible
. Output possible
if there exists a rearrangement of the rows such that every column is sorted in non-decreasing order, and impossible
otherwise.## sample
3 3
cba
fgh
ijk
possible
</p>