#P9503. 『MGOI』Simple Round I | B. 魔法照相馆

『MGOI』Simple Round I | B. 魔法照相馆

Background

Photos capture moments worth remembering, but for mages, the most important thing is to look forward. — Hall Mage W.

Problem Description

Little M is preparing the mage documents required for enrollment, so he comes to the Magic Photo Studio in Deep Alley.

While waiting, Little M notices that the studio has three curtains. From left to right, their colors are red, blue, and white. The owner, Mr. zx, will pull up or pull down these three curtains according to the customer’s needs, as shown in the figure below:

The curtains are placed on the left, arranged in the order red, blue, white. The person sits to the right of the curtains, and Mr. zx stands at the far right to take the photo. Counting from right to left, the color of the first curtain that is not pulled up will become the background color of the photo.

Each time Mr. zx pulls up or pulls down one curtain, it costs one unit of time, and taking a photo costs no time. Little M already knows the requirements of all nn customers in front of him. Given that the initial state is all pulled down, please help Little M compute the minimum number of time units he has to wait.

Input Format

The first line contains an integer nn.

The second line contains a string of length nn consisting of three uppercase letters R, B, W. The ii-th character indicates the ii-th customer’s requirement: R means red, B means blue, and W means white.

Output Format

Output one integer in one line, indicating the minimum number of time units Little M has to wait.

5
BBRWB
5
10
BBBBBBBBBB
1

Hint

Sample 1 Explanation.

At the beginning, all three curtains are pulled down.

  • The first customer wants blue, so pull up the white curtain, costing one unit of time.
  • The second customer wants blue, so no change is needed.
  • The third customer wants red, so based on the previous state, pull up the blue curtain, costing one unit of time.
  • The fourth customer wants white, so pull down the white curtain, costing one unit of time.
  • The fifth customer wants blue, so pull up the white curtain and pull down the blue curtain, costing two units of time.

A total of five time units are spent.

Constraints.

This problem has five test points.

Test Point ID n=n = Special Property
11 None
22 10001000 Yes
33 None
44 10510^5 Yes
55 None

Special property: only the two colors W and B appear.

Translated by ChatGPT 5