0% found this document useful (0 votes)
44 views1 page

Permalex: Input

The document describes how to order permutations of a string alphabetically and assign each permutation a unique number corresponding to its position in the ordering. It provides an example where the string "acab" has 12 permutations numbered 1 through 12. The task is to write a program that takes a string as input and outputs the number corresponding to its permutation sequence position. The program must handle strings of up to 30 lowercase letters and output numbers less than 2^31-1.

Uploaded by

Hello mister
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views1 page

Permalex: Input

The document describes how to order permutations of a string alphabetically and assign each permutation a unique number corresponding to its position in the ordering. It provides an example where the string "acab" has 12 permutations numbered 1 through 12. The task is to write a program that takes a string as input and outputs the number corresponding to its permutation sequence position. The program must handle strings of up to 30 lowercase letters and output numbers less than 2^31-1.

Uploaded by

Hello mister
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

153 Permalex

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 231 − 1 = 2, 147, 483, 647.

Input
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. The file will be terminated by a line consisting of a single
‘#’.

Output
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
#

Sample Output
15
1
6

You might also like