Passwords
Time limit1.5sMemory limit64 MB
Given n rows of m letters, permute the columns so the rows become lexicographically nondecreasing, choosing the smallest such permutation or reporting NIE.
- Level
Hard8 of 10
- Topics
- Greedy, Sorting, String, Implementation
- Solved
- No attempts yet
Problem
Johnny is obsessed with computer security: he has a different password for each website, he destroys the printouts, and so on. And this is his demise: he realised that he accidentally put the sheet with his passwords to the paper shredder. But what are the odds, this sheet of paper was shredded so that each piece of paper corresponds to one column of text. Moreover, Johnny knows for sure that all passwords consist only of capital letters of the English alphabet, they are pairwise different, they all have the same length, and they were written down in the lexicographic order. Johnny numbered the columns and put them side by side but he is not sure whether the order he came up with is correct. Help Johnny. Write a program that computes how to permute the columns of the text so that the words written in the rows are lexicographically ordered. If this is possible for many different permutations, choose the one which is lexicographically smallest.
Input
The first line of the input contains two natural numbers (), separated by spaces. The following lines contain words, one per line. Each one of them consists of capital letters of the English alphabet.
Output
Write natural numbers in one line: the permutation of the columns after which the words in the rows are sorted lexicographically. If there are many such permutations, write the one that is lexicographically smallest among them. If there is no such permutation, write "NIE" (Polish for 'no') instead.
Notes
In Sample 1, after permuting the columns in the described way we obtain the words "MTOEK" and "SKAIA", which are lexicographically sorted.
In Sample 2 there is no way to permute the columns so that the words obtained in the rows are lexicographically sorted.