#P13022. [GCJ 2021 Qualification] Moons and Umbrellas

    ID: 12807 Type: RemoteJudge 10000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>动态规划 DP2021Google Code Jam

[GCJ 2021 Qualification] Moons and Umbrellas

题目描述

Cody-Jamal is working on his latest piece of abstract art: a mural consisting of a row of waning moons and closed umbrellas. Unfortunately, greedy copyright trolls are claiming that waning moons look like an uppercase C and closed umbrellas look like a J, and they have a copyright on CJ and JC. Therefore, for each time CJ appears in the mural, Cody-Jamal must pay X\mathbf{X}, and for each time JC appears in the mural, he must pay Y\mathbf{Y}.

Cody-Jamal is unwilling to let them compromise his art, so he will not change anything already painted. He decided, however, that the empty spaces he still has could be filled strategically, to minimize the copyright expenses.

For example, if CJ?CC? represents the current state of the mural, with C representing a waning moon, J representing a closed umbrella, and ? representing a space that still needs to be painted with either a waning moon or a closed umbrella, he could finish the mural as CJCCCC, CJCCCJ, CJJCCC, or CJJCCJ. The first and third options would require paying X+Y\mathbf{X} + \mathbf{Y} in copyrights, while the second and fourth would require paying 2X+Y2 \cdot \mathbf{X} + \mathbf{Y}.

Given the costs X\mathbf{X} and Y\mathbf{Y} and a string representing the current state of the mural, how much does Cody-Jamal need to pay in copyrights if he finishes his mural in a way that minimizes that cost?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} lines follow. Each line contains two integers X\mathbf{X} and Y\mathbf{Y} and a string S\mathbf{S} representing the two costs and the current state of the mural, respectively.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum cost that Cody-Jamal needs to pay in copyrights for a finished mural.

4
2 3 CJ?CC?
4 2 CJCJ
1 3 C?J
2 5 ??J???
Case #1: 5
Case #2: 10
Case #3: 1
Case #4: 0
1
2 -5 ??JJ??
Case #1: -8

提示

Sample Explanation

Sample Case #1 is the one explained in the problem statement. The minimum cost is X+Y=2+3=5\mathbf{X} + \mathbf{Y} = 2 + 3 = 5.

In Sample Case #2, Cody-Jamal is already finished, so he does not have a choice. There are two CJs and one JC in his mural.

In Sample Case #3, substituting either C or J results in one CJ either from the second and third characters or the first and second characters, respectively.

In Sample Case #4, Cody-Jamal can finish his mural with all Js. Since that contains no instance of CJ nor JC, it yields no copyright cost.

The following additional sample 2 fits the limits of Test Set 3. It will not be run against your submitted solutions.

In Sample Case #1 for Test Set 3, Cody-Jamal can finish his mural optimally as JCJJCC or JCJJJC. Either way, there is one CJ and two JCs in his mural.

Limits

  • 1T1001 \leq \mathbf{T} \leq 100.
  • Each character of S\mathbf{S} is either c\mathbf{c}, J\mathbf{J}, or ?\mathbf{?}.

Test Set 1 (5 Pts, Visible Verdict)

  • 11 \leq the length of S10\mathbf{S} \leq 10.
  • 1X1001 \leq \mathbf{X} \leq 100.
  • 1Y1001 \leq \mathbf{Y} \leq 100.

Test Set 2 (11 Pts, Visible Verdict)

  • 11 \leq the length of S1000\mathbf{S} \leq 1000.
  • 1X1001 \leq \mathbf{X} \leq 100.
  • 1Y1001 \leq \mathbf{Y} \leq 100.

Extra credit!

What if some copyright holders could pay Cody-Jamal for the advertisement instead of being paid? Cody-Jamal getting paid is represented by a negative cost.

Test Set 3 (1 Pts, Hidden Verdict)

  • 1<1 < the length of S<1000\mathbf{S} < 1000.
  • 100X100-100 \leq \mathbf{X} \leq 100.
  • 100Y100-100 \leq \mathbf{Y} \leq 100.