#P16866. [GKS 2021 #H] Painter

[GKS 2021 #H] Painter

Problem Description

You have recently started to study how to paint, and in one of the first classes you learned about the three primary colors: Red, Yellow, and Blue. If you combine these colors, you can produce many more colors. For now, the combinations that you have studied are the following:

  • Red+Yellow=Orange\text{Red} + \text{Yellow} = \text{Orange}
  • Red+Blue=Purple\text{Red} + \text{Blue} = \text{Purple}
  • Yellow+Blue=Green\text{Yellow} + \text{Blue} = \text{Green}
  • $\text{Red} + \text{Yellow} + \text{Blue} = \text{Gray}$

You still do not understand shades of colors, therefore the proportion and order of each color in the combination does not matter. For example, combining Red and Yellow produces the same result as combining Yellow and Red, as well as the same result as combining Red, Yellow, and Red again.

To practice your skills, you want to paint a 11-dimensional painting PP of length NN. Your painting consists of NN squares. From left to right, PiP_i represents the color of the ii-th square. Initially all squares are Uncolored, that is, Pi=UncoloredP_i = \text{Uncolored} for every 1iN1 \le i \le N.

In a single stroke, you can choose one of the three primary colors and apply it to a sequence of consecutive squares. In other words, you can choose a color cc and two integers ll and rr, such that 1lrN1 \le l \le r \le N, and apply color cc to all squares PjP_j such that ljrl \le j \le r. If the square being painted is currently Uncolored, then its color will become cc. Otherwise, the color will be a combination of all the colors applied on this square so far and the new color cc, as described in the list above.

In order to save time, you want to use as few strokes as possible. Given the description of the painting that you want to paint, figure out what is the minimum number of strokes required to paint it.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case starts with a line containing an integer NN, representing the length of the painting. Then on the next line, there will be a string PP of length NN, representing the painting. The ii-th character represents the color of square PiP_i, according to the following list:

  • U=UncoloredU = \text{Uncolored}
  • R=RedR = \text{Red}
  • Y=YellowY = \text{Yellow}
  • B=BlueB = \text{Blue}
  • O=OrangeO = \text{Orange}
  • P=PurpleP = \text{Purple}
  • G=GreenG = \text{Green}
  • A=GrayA = \text{Gray}

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the minimum number of strokes required to paint the painting.

2
9
YYYBBBYYY
6
YYGGBB
Case #1: 3
Case #2: 2
1
5
ROAOR
Case #1: 3

Hint

Limits

1T1001 \le T \le 100.

1N1051 \le N \le 10^5.

Test Set 11

PiP_i will be one of {Y,B,G}\{Y, B, G\}.

Test Set 22

PiP_i will be one of {U,R,Y,B,O,P,G,A}\{U, R, Y, B, O, P, G, A\}.