ACGU

Time limit2sMemory limit128 MB

Summary
Given an RLE-encoded RNA-like string, find the maximum number of non-crossing A-U and C-G pairs with at most K C-G pairs, exploiting the special RLE size constraints.
Level

Hard8 of 10

Topics
Dynamic programming, String, Combinatorics
Solved
No attempts yet

Problem

You are given a string SS over the alphabet {A,C,G,U}\{A, C, G, U\}. You may form pairs between characters of SS under the rules below, and you want to make as many pairs as possible.

A pairing must satisfy all of the following.

  1. An AA may be paired with a UU.
  2. A CC may be paired with a GG.
  3. Each character is paired with at most one other character.
  4. Suppose the ww-th character is paired with the xx-th character and the yy-th character is paired with the zz-th character, where w<xw < x, y<zy < z, and w<yw < y. Then at least one of y>xy > x or z<xz < x must hold. That is, no two pairs may cross.
  5. At most KK of the pairs may be CC-GG pairs.

The string SS is given in run-length encoded (RLE) form. RLE writes each run of consecutive identical characters as that character followed by the number of times it repeats; for example, AAAACCGAAUUG is encoded as A4C2G1A2U2G1. Formally the input has the form c1f1c2f2…cnfnc_1 f_1 c_2 f_2 \ldots c_n f_n, where each ci∈{A,C,G,U}c_i \in \{A, C, G, U\} and each fif_i is a positive integer.

The encoded string satisfies all of the following.

  • f1+f2+⋯+fn≤10050f_1 + f_2 + \cdots + f_n \le 10050
  • f1≤5000f_1 \le 5000
  • fn≤5000f_n \le 5000
  • f2+f3+⋯+fn−1≤50f_2 + f_3 + \cdots + f_{n-1} \le 50

Compute the maximum number of pairs that can be formed.

Input

The first line contains the number of test cases TT (T≤200T \le 200). Each test case is given on two lines: the first line contains the string SS in RLE form, and the second line contains the integer KK (0≤K≤200 \le K \le 20).

Output

For each test case, print a line of the form Case i: p, where ii is the test case number (starting from 11) and pp is the maximum number of pairs that can be formed.

Hint

In the first example, six pairs can be formed.

Examples1

  1. Example 1

    Input
    3
    A3C1G1C1U4A2U1
    1
    A3C1G1C1U4A2U1
    0
    A100U200
    2
    
    Expected output
    Case 1: 6
    Case 2: 5
    Case 3: 100