#abc240h. Ex - Sequence of Substrings

Ex - Sequence of Substrings

Score : 600600 points

问题描述

给定一个长度为 NN 的字符串 S=s1s2sNS = s_1 s_2 \ldots s_N,其中包含 0011

找出满足以下三个条件的最大整数 KK,以及对应的序列 $\big((L_1, R_1), (L_2, R_2), \ldots, (L_K, R_K)\big)$。

  • 对于每个 i=1,2,,Ki = 1, 2, \ldots, K,有 1LiRiN1 \leq L_i \leq R_i \leq N
  • 对于 i=1,2,,K1i = 1, 2, \ldots, K-1,满足 Ri<Li+1R_i < L_{i+1}
  • 字符串 sLisLi+1sRis_{L_i}s_{L_i+1} \ldots s_{R_i} 在字典序上严格小于字符串 sLi+1sLi+1+1sRi+1s_{L_{i+1}}s_{L_{i+1}+1}\ldots s_{R_{i+1}}

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a string S=s1s2sNS = s_1 s_2 \ldots s_N of length NN consisting of 00's and 11's.

Find the maximum integer KK such that there is a sequence of KK pairs of integers $\big((L_1, R_1), (L_2, R_2), \ldots, (L_K, R_K)\big)$ that satisfy all three conditions below.

  • 1LiRiN1 \leq L_i \leq R_i \leq N for each i=1,2,,Ki = 1, 2, \ldots, K.
  • Ri<Li+1R_i \lt L_{i+1} for i=1,2,,K1i = 1, 2, \ldots, K-1.
  • The string sLisLi+1sRis_{L_i}s_{L_i+1} \ldots s_{R_i} is strictly lexicographically smaller than the string sLi+1sLi+1+1sRi+1s_{L_{i+1}}s_{L_{i+1}+1}\ldots s_{R_{i+1}}.

Constraints

  • 1N2.5×1041 \leq N \leq 2.5 \times 10^4
  • NN is an integer.
  • SS is a string of length NN consisting of 00's and 11's.

Input

Input is given from Standard Input in the following format:

NN

SS

Output

Print the answer.

Sample Input 1

7
0101010

Sample Output 1

3

For K=3K = 3, one sequence satisfying the conditition is $(L_1, R_1) = (1, 1), (L_2, R_2) = (3, 5), (L_3, R_3) = (6, 7)$. Indeed, s1=0s_1 = 0 is strictly lexicographically smaller than s3s4s5=010s_3s_4s_5 = 010, and s3s4s5=010s_3s_4s_5 = 010 is strictly lexicographically smaller than s6s7=10s_6s_7 = 10.
For K4K \geq 4, there is no sequence $\big((L_1, R_1), (L_2, R_2), \ldots, (L_K, R_K)\big)$ satisfying the condition.

Sample Input 2

30
000011001110101001011110001001

Sample Output 2

9

update @ 2024/3/10 10:22:36