You are not logged in. Please login at www.codechef.com to post your questions!

×

PLSTR - Editorial

PROBLEM LINK:

Practice Contest

Author: Ayush Nagal

DIFFICULTY:

MEDIUM

PREREQUISITES:

Math

PROBLEM:

Given a string $S$, find the rank of the string amongst its permutations sorted lexicographically. Assume that no characters are repeated.

EXPLANATION:

It is a well-known problem of Permutation and Combination. First of all, we need an auxiliary array (like Counting Sort) to store the frequency of all the characters in the given string. It can be obtained as follows:

map <char,int> m;
map <char,int>::iterator it;

for(i=0;i < n;i++){
    m[B[i]] = i;
}

After obtaining the auxiliary array, we can use the following code to obtain the rank:

while(fix < n)
{
    r = m[A[fix]];
    ans = ans + ((r)*fact(n-1-fix))%mod;
    if(ans > mod)
    ans = ans - mod;

    for(it=m.begin();it!=m.end();it++)
    {
        if((*it).first>A[fix])
        {
            (*it).second -= 1;
        }
    }
    fix++;
}

The rank will be $ans+1$.

AUTHOR'S SOLUTION:

Author's solution can be found here.

This question is marked "community wiki".

asked 08 Mar, 14:08

ayush163's gravatar image

3★ayush163
11
accept rate: 0%

edited 14 Mar, 06:42

admin's gravatar image

0★admin ♦♦
19.8k350498541

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,852
×2,657
×1,220
×9
×2

question asked: 08 Mar, 14:08

question was seen: 53 times

last updated: 14 Mar, 06:42