2393: Arranging Books

Memory Limit:128 MB Time Limit:1.000 S
Creator:
Submit:3 Solved:0

Description

Valentina wants books on a shelf to be arranged in a particular way. Every time she sees a shelf of books, she rearranges the books so that all the large books appear on the left, followed by all the medium-sized books, and then all the small books on the right. She does this by repeatedly choosing any two books and exchanging their locations. Exchanging the locations of two books is called a swap.Help Valentina determine the fewest number of swaps needed to arrange a shelf of books as she wishes.
瓦伦蒂娜希望书架上的书籍按照特定的方式排列。每当她看到一排书时,她就会重新排列这些书籍,使所有大本书都放在左边,其次是所有中等大小的书,然后是右边的所有小本书。她通过反复选择任意两本书并交换它们的位置来实现这一点。交换两本书的位置被称为一次交换。 帮助瓦伦蒂娜确定安排一排书籍如她所愿所需的最少交换次数。

Input

The input will consist of exactly one line containing at least one character.
输入将由且仅由一行组成,包含至少一个字符。
The following table shows how the available 15 marks are distributed. 

Output

Output a single integer which is equal to the minimum possible number of swaps needed to arrange the books so that all occurrences of L come first, followed by all occurrences of M,
and then all occurrences of S.
输出一个单独的整数,等于安排书籍的最少交换次数,使所有L出现在首位,其次是所有M的出现,然后是所有S的出现。

Sample Input

LLSLM

Sample Output

2

HINT

Explanation of Output for Sample
By swapping the S and M, we end up with LLMLS. Then the M can be swapped with the L to its right. This is one way to use two swaps to arrange the books as Valentina wants them to be. It is not possible to use fewer swaps because both S and M need to be moved but usingone swap to exchange them does not leave the books arranged as Valentina wants them tobe.
输出解释
通过交换S和M,我们得到LLMLS。然后可以将M与其右侧的L交换。这是一种用两次交换来安排书籍的方法,使之符合瓦伦蒂娜的意愿。由于S和M都需要移动,但使用一次交换来交换它们并不能使书籍按瓦伦蒂娜希望的方式排列,因此不可能用更少的交换次数完成。

Source/Category