Lexicographic rows

3

1 votes
Basic Programming, Medium, Constructive algorithm, Implementation, Constructive Algorithms, Basics of Implementation
Problem

You are given a N×M matrix of characters. In one operation, you can remove a column of the matrix. You can perform as many operations as you want. Your task is to make the final matrix interesting. An interesting string is the string that is formed by the characters of ith row is lexicographically smaller or equal to the string that is formed by the characters of the (i+1)th row. You are required to use the minimum number of operations possible.
An empty matrix is always an interesting matrix.

Input format

  • First line: Two integers Nand M
  • Next N lines: M letters each

Output format
Print the minimum number of operations that are required to make a matrix interesting.

Constraints
1N,M100

There are only lowercase English alphabets as characters in the input.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Delete the first column to make the matrix interesting.

Editor Image

?