Word ranking efficiency

I am not sure how to solve this problem within the constraints.

Consider a "word" as any sequence of capital letters A-Z (not limited to just "dictionary words"). For any word with at least two different letters, there are other words composed of the same letters but in a different order (for instance, STATIONARILY/ANTIROYALIST, which happen to both be dictionary words; for our purposes "AAIILNORSTTY" is also a "word" composed of the same letters as these two). We can then assign a number to every word, based on where it falls in an alphabetically sorted list of all words made up of the same set of letters. One way to do this would be to generate the entire list of words and find the desired one, but this would be slow if the word is long. Write a program which takes a word as a command line argument and prints to standard output its number. Do not use the method above of generating the entire list. Your program should be able to accept any word 25 letters or less in length (possibly with some letters repeated), and should use no more than 1 GB of memory and take no more than 500 milliseconds to run. Any answer we check will fit in a 64-bit integer.

Sample words, with their rank:

ABAB = 2 
AAAB = 1 
BAAA = 4 
QUESTION = 24572 
BOOKKEEPER = 10743

examples:

AAAB - 1
AABA - 2
ABAA - 3
BAAA - 4

AABB - 1
ABAB - 2
ABBA - 3
BAAB - 4
BABA - 5
BBAA - 6

I thought about using a binary search for a word and all the possible words built from the characters (1 - permutation(word)) but I think that would take too long. O(logN) might be too slow.

I found this solution but I am a bit confused and need a bit of help understanding it:

Consider the n-letter word { x1, x2, ... , xn }. My solution is based on the idea that the word number will be the sum of two quantities:

The number of combinations starting with letters lower in the alphabet than x1, and
how far we are into the the arrangements that start with x1.
The trick is that the second quantity happens to be the word number of the word { x2, ... , xn }. This suggests a recursive implementation.

Getting the first quantity is a little complicated:

Let uniqLowers = { u1, u2, ... , um } = all the unique letters lower than x1
For each uj, count the number of permutations starting with uj.
Add all those up.

Answers


The solutions says that the answer consists of two numbers. Look at the following picture describing the words that can be made from the word QUESTION:

EIONQSTU (first word lexographically, rank 1)
...
...
... (first word before Q, rank A)
QEIONSTU 
....
....
QUESTION (our given word, rank x)
...

This phrase "how far we are into the the arrangements that start with x1", is the quantity (x-A), call it B. The thing is B is exactly equal to the word rank of "UESTION", which is our original word with the first letter cut off. This is asking the same question but with a subset of our input, suggesting a recursive solution.

It then remains to find A, this says to find the number of permutations of words beginning with words that come before Q. So A = number of words beginning with {E, I, O, N}


Need Your Help

pixel text to 3d mesh convert library

c++ text mesh

Via GDI/GDI+ get the text pixels or glyph, how to convert to 3d mesh? Does any exists library or source code can be used?

How to replace variable with string in jquery flot graph

javascript jquery asp.net flot

I am using Flot line graph which is working alright:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.