#P4763. Uniform Brick Partition
Uniform Brick Partition
Uniform Brick Partition
You are given a sequence of bricks represented as a string consisting of the characters W
(white) and B
(black). The goal is to partition the sequence into the maximum possible number of non‐empty, contiguous blocks such that each block has the same ratio of white to black bricks.
For example:
BWWWBB
can be partitioned asBW
+WWBB
(each block has a ratio of 1:1).WWWBBBWWWWWWWWWB
can be partitioned asWWWB
+BBWWWWWW
+WWWB
(each block has a ratio of 3:1).
Note: If the sequence is composed of only one color then every single brick can form a block. In all other cases, it is guaranteed that the total counts of white and black are both positive.
Hint: If the overall counts of white and black bricks are W and B respectively, let g = \(\gcd(W,B)\) and the basic ratio be \(\left(\frac{W}{g}:\frac{B}{g}\right)\). Then, by scanning from left to right and tracking the count of each brick, one may partition the sequence each time the current counts form an integer multiple of the basic ratio.
inputFormat
The input consists of a single line containing a non-empty string s
composed solely of the characters W
and B
, representing white and black bricks respectively.
outputFormat
Output a single integer, the maximum number of contiguous blocks into which the sequence can be partitioned so that each block has the same ratio of white to black bricks.
sample
BWWWBB
2