What exactly is "lexicographical order"?
Moderator: Board moderators
-
- New poster
- Posts: 14
- Joined: Tue Feb 03, 2004 3:43 am
What exactly is "lexicographical order"?
There are quite a few questions where we are required to print the lexicographically smallest answer. Is there a good way in general to get this smallest answer? Problems that are like this include 116, 10026...
Re: huh
What you are explaining is a particular case of lexicographic order.sohel wrote:From what I know :
a string A is lexicographically smaller than a string B
if( strcmp(A, B) < 0 );
EG: AB is smaller than Aa....
For alphabetically: sorted string is :
AaBbCcDd.........
and so Aa is alphabetically smaller than AB.
Lexicographic means that you sort different objects considering first an attribute ranked the most important.
If you have a tie for the most important attribute you further sort checking the second ranked attribute and so on until you find an attribute that distinguishes the two objects.
To sort lexicographically you need to specify an ordered list of attributes and a sort order for the elements of each attribute.
The most usual implementation of a lexicographic order is the classic dictionary order.
The list of attrbutes is (first letter, second letter, third letter, etc.) and the sort order for each attribute is the alphabetical order.
111 is smaller that 22 in the classical lexicographic order since its first cypher (1) is smaller than the first cypher of 22.sohel wrote:And for numbers : maintain an ascending order..
111 is larger than 22 ( I think ).
About the original question, the best way to find the smallest lexicographical solution is to generate the solutions in lexicographical order

Ciao!!!
Claudio