No.38 Red, Blue, and White Blocks
Problem Statement
There are 10 blocks each of red, blue, and white.
The blocks are arranged in a single row from left to right.
The arrangement must follow these rules.
There must not be a red block exactly Kr blocks to the left of a red block.
There must not be a red block exactly Kr blocks to the right of a red block.
There must not be a blue block exactly Kb blocks to the left of a blue block.
There must not be a blue block exactly Kb blocks to the right of a blue block.
There are no particular restrictions on white blocks.
First, the values of Kr and Kb and the arrangement of the blocks are given.
A red block is denoted by R, a blue block by B, and a white block by W.
The initial state may not satisfy the conditions above.
Therefore, the following operation is allowed.
・Remove some of the red or blue blocks and close the gaps in the row.
For example, from "RWBRWWB",
you can remove only the leftmost R to make the row "WBRWWB".
You can also remove the 4th R from the left and the rightmost B to make the row "RWBWW".
You may choose and remove any number of Rs and Bs.
However, W blocks cannot be removed.
By performing such an operation, you want to leave the longest possible row that satisfies the conditions.
You are free to choose how to remove the R and B blocks.
What is the length of the longest row that can be left?
Input
Kr Kb S
1<=Kr,Kb<=29
S is a string consisting of 10 Rs, 10 Bs, and 10 Ws.
Output
Ans
Print Ans, the maximum number of remaining blocks.
Do not forget the newline at the end.
Sample
Sample 1
Input
1 10 RRRRRRRRRRWWWWWWWWWWBBBBBBBBBB
Output
21
The block 1 position next to an R must not be an R, so 9 Rs are removed.
No B has a B 10 positions away from it, so no B needs to be removed.
In the end, RWWWWWWWWWWBBBBBBBBBB remains.
Sample 2
Input
1 2 WRWWBBRRRWWBBRRRRWWWBBBRRWWBBB
Output
22
Sample 3
Input
7 11 BWBWRWRRWBRRWRRWWBBBRRWBBRWBBW
Output
27
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
nmnmnmnmnmnmnm