You will often encounter a problem similar to the following:
Give you the number of n, composed of 0 and 1, Q 1 or 0 of the maximum continuous length is how much
The maximum continuous length of 110001101 is the median of 3 consecutive 0, so it is 3
Here is a slightly summed up a ruler method, after encountering such a problem will not need to think too much details
int L, R = 1, ans = 0;for (l = 1; L <= N; L = R + 1) {for (r = L; R + 1 <= n && b[l] = = b[r + 1]; r++); ans = max (ans, r-l + 1);}
where n is the length of the array, the subscript starts at 1, and b represents the corresponding array
So the ANS is the maximum continuous length.
If it is necessary to deal with the continuous interval of 1, you just need to add the new ans to judge the sentence, to determine whether one of the interval is 1, and then to deal with it.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to deal with the same number in continuous interval by taking the ruler method