#P12143. Count Good Substrings
Count Good Substrings
Count Good Substrings
Given a numeric string consisting only of digits 0–9, a substring is defined as follows. For a string \(s = s_0s_1 \cdots s_{n-1}\) of length \(n\), any substring is obtained by choosing two indices \(l, r\) with \(0 \le l \le r \le n-1\) and taking \(s' = s_ls_{l+1}\cdots s_r\).
A substring is called a good substring if and only if it satisfies one of the following conditions:
- If the substring has length 1, it is always good.
- If the substring has length greater than 1, it can be split into two contiguous segments such that each segment is a contiguous non-decreasing substring.
A string \(p = p_0p_1\cdots p_{k-1}\) is a contiguous non-decreasing substring if for every index \(1 \le i < k\) the digit \(p_i\) is either equal to \(p_{i-1}\) or equal to \(p_{i-1}+1\) (in its integer value). For example, \(\texttt{12233}\) and \(\texttt{456}\) are contiguous non-decreasing substrings.
Your task is to count the number of good substrings among all the substrings of the given numeric string.
inputFormat
The input consists of a single line containing a non-empty string made up solely of digits (0–9).
outputFormat
Output a single integer representing the number of good substrings.
sample
1234
10
</p>