ACGU
Time limit2sMemory limit128 MB
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 over the alphabet . You may form pairs between characters of under the rules below, and you want to make as many pairs as possible.
A pairing must satisfy all of the following.
- An may be paired with a .
- A may be paired with a .
- Each character is paired with at most one other character.
- Suppose the -th character is paired with the -th character and the -th character is paired with the -th character, where , , and . Then at least one of or must hold. That is, no two pairs may cross.
- At most of the pairs may be - pairs.
The string 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 , where each and each is a positive integer.
The encoded string satisfies all of the following.
Compute the maximum number of pairs that can be formed.
Input
The first line contains the number of test cases (). Each test case is given on two lines: the first line contains the string in RLE form, and the second line contains the integer ().
Output
For each test case, print a line of the form Case i: p, where is the test case number (starting from ) and is the maximum number of pairs that can be formed.
Hint
In the first example, six pairs can be formed.