CF873B.Balanced Substring
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string s__l__s__l + 1_s__l_ + 2... s__r, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.
You have to determine the length of the longest balanced substring of s.
给你一个仅由字符 0 和 1 组成的字符串 s。s 的一个子串 [l,r] 定义为字符串 slsl+1sl+2…sr,其长度为 r−l+1。若一个子串中 0 的个数等于 1 的个数,则称该子串是平衡的。
你需要求出 s 的最长平衡子串的长度。
输入格式
The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.
The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.
第一行包含一个整数 n(1 ≤ n ≤ 100000)——表示字符串 s 的字符个数。
第二行包含一个长度恰好为 n 的字符串 s。字符串 s 中仅包含字符 0 和 1。
输出格式
If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.
如果字符串 s 中不存在非空的平衡子串,则输出 0;否则,输出最长平衡子串的长度。
输入输出样例
输入#1
8 11010111
输出#1
4
输入#2
3 111
输出#2
0
说明/提示
In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.
In the second example it's impossible to find a non-empty balanced substring.
在第一个例子中,你可以选择子串 [3, 6]。它是平衡的,且长度为 4。选择子串 [2, 5] 也是可行的。
在第二个例子中,无法找到一个非空的平衡子串。
输入解题思路,AI测评打分。不知道怎么写?