#K57762. Longest Non-Consecutive Subsequence

    ID: 30493 Type: Default 1000ms 256MiB

Longest Non-Consecutive Subsequence

Longest Non-Consecutive Subsequence

Given a binary string S, determine the length of the longest subsequence that can be formed such that no two consecutive elements in the subsequence are the same. A subsequence is obtained by deleting zero or more characters from S without changing the order of the remaining characters.

For example, consider the string S = "1101001". One valid subsequence is "10101" which has length 5. In mathematical terms, if we denote the answer as L, then

$$L = \begin{cases} 0, & \text{if } |S| = 0,\\ 1 + \sum_{i=1}^{|S|-1} \mathbb{1}(S_i \neq S_{i-1}), & \text{otherwise}, \end{cases} $$

Here, \(\mathbb{1}(\cdot)\) is the indicator function that is 1 if the condition is true, and 0 otherwise.

Some examples:

  • S = "11111" returns 1.
  • S = "10101010" returns 8.
  • S = "101" returns 3.
  • S = "" (an empty string) returns 0.

inputFormat

The input consists of a single line containing the binary string S. The string only contains the characters '0' and '1'.

outputFormat

Output a single integer representing the length of the longest subsequence of S in which no two consecutive characters are the same.

## sample
1101001
5