Editorial for Collar Roto.
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Analysis
Given an input string , we concatenate it with itself, result in
. This trick, working on handling minus or over-length index is easier.
Brute force solution.
For each index as a start, if
can be picked into the longest substring, we pick it then do this again on
. Note that, if
is w, we must treat it as b and r, one by one. We do
times of picking attempt for each
. Therefore complexity is
. Just a straight forward solution.
DP solution.
- We denote substring composed by successive
, which ends at index
to
.
- Similarly, we denote substring composed by successive
, which ends at index
to
.
- On the other hand, we denote substring composed by successive
, which starts at index
to
.
- And substring composed by successive b, which starts at index
to
.
For each index ,
is the length of longest substring if we break it at index
. We do this for
times, the greatest one is the answer. Complexity of this algorithm is
.
Comments