Under construction

Still being built. Things may change

Moons and Umbrellas

Time limit10sMemory limit1024 MB

Summary
Fill each ? in a string of C, J, and ? with C or J to minimize the total cost, where each CJ substring costs X and each JC substring costs Y.
Level

Medium4 of 10

Topics
Greedy, Dynamic programming, String, Implementation
Solved
No attempts yet

Problem

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, and for each time JC appears in the mural, he must pay 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 in copyrights, while the second and fourth would require paying 2⋅X+Y.

Given the costs X and 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?

Input

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

Output

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

Limits

  • 1 ≤ T ≤ 100.
  • Each character of S is either C, J, or ?.

Examples2

  1. Example 1

    Input
    4
    2 3 CJ?CC?
    4 2 CJCJ
    1 3 C?J
    2 5 ??J???
    
    Expected output
    Case #1: 5
    Case #2: 10
    Case #3: 1
    Case #4: 0
    
  2. Example 2

    Input
    1
    2 -5 ??JJ??
    
    Expected output
    Case #1: -8