PRMLX - Permalex

no tags 

Given a string of characters, we can permute the individual characters to make new strings. If we can impose an ordering on the characters (say alphabetic sequence), then the strings themselves can be ordered and any given permutation can be given a unique number designating its position in that ordering. For example the string 'acab' gives rise to the following 12 distinct permutations:

aabc 1 acab 5 bcaa 9
aacb 2 acba 6 caab 10
abac 3 baac 7 caba 11
abca 4 baca 8 cbaa 12

Thus the string 'acab' can be characterised in this sequence as 5.

Write a program that will read in a string and determine its position in the ordered sequence of permutations of its constituent characters. Note that numbers of permutations can get very large; however we guarantee that no string will be given whose position is more than 2^31 = 2,147,483,647

Input and Output

Input will consist of a series of lines, each line containing one string. Each string will consist of up to 30 lower case letters, not necessarily distinct. Test cases a separated by empty lines. The file will be terminated by a line consisting of a single #.

Output will consist of a series of lines, one for each line of the input. Each line will consist of the position of the string in its sequence, right justified in a field of width 10.

Sample Input

bacaa
abc
cba

bacaa
abc
cba
#

Sample Output

        15
         1
         6

        15
         1
         6

hide comments
nadstratosfer: 2019-07-12 07:49:20

Found out I had a sneaky, barely detectable bug in computation but only after 3 hours of experimenting with I/O methods, thinking my solution wasn't handling the stupid format correctly. Where were the mods when a turd like this was published...

Last edit: 2019-07-12 09:41:17
:D: 2009-06-16 19:50:15

It was discussed on the forum. But it should be noted here as well. For every empty line print "1" justified like the rest of answers. The sample output has an error in the 4th line.

Peres: 2009-04-13 22:19:22

2^31 is not 2.147.483.647 ;)


Added by:Andrés Leonardo Rojas Duarte
Date:2007-07-27
Time limit:1.614s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ACM ICPC 1992 Northwestern European Regionals