Balanced Substring

You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, 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.

Input

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.

Output

If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.

Example

Input

811010111

Output

4

Input

3111

Output

0

Note

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.

求前缀和,然后map一下,前缀和相同的表示两位置之间的字符串达到了平衡。

代码:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int s[100001];
int main()
{
    int n;
    string a;
    cin>>n;
    cin>>a;
    map<int,int>q;
    int sum = 0,maxi = 0;
    for(int i = 0;i < n;i ++)
    {
        if(a[i] == ‘0‘)sum += -1;
        else sum += 1;
        if(!q[sum]&&sum)q[sum] = i + 1;//如果sum为0表示本来就平衡不需要标记了
        else
        {
            maxi = max(maxi,i - q[sum] + 1);
        }
    }
    cout<<maxi;
}
时间: 2024-10-30 06:41:59

Balanced Substring的相关文章

Codeforces 873 B. Balanced Substring(前缀和 思维)

题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置.因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度.但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0.我就是在这个地方错了,这里给出错地方的数据. 1 #include<bi

CodeForces - 873B Balanced Substring(思维)

inputstandard input outputstandard output You are given a string s consisting only of characters 0 and 1. A substring [l,?r] of s is a string slsl?+?1sl?+?2- sr, and its length equals to r?-?l?+?1. A substring is called balanced if the number of zero

CF873B Balanced Substring (前缀和)

CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] - \sum[l][0] = \sum[i][1] - \sum[l][1]\) \(\sum[i][0] - \sum[l][1] = \sum[i][0] - \sum[l][0]\) 然后hash一下DP即可. #include <iostream> #include <cstdio

前缀和,二维前缀和!

前缀和 定义 用空间换取效率,做一个预处理,然后可以\(O(1)\)的查询某个区间的值的和. 实现 设\(s_i\)为第\(i\)个数\(a_i\)的前缀和,则\(s_i=s_i-1+a_i\) 当要查找区间的和时只要把对应的起点终点的元素相减即可. 例题 Educational Codeforces Round 30B Balanced Substring 翻译 给你一个长度至多为\(100000\)的\(01\)串,其中含有相同\(0\),\(1\)个数的子串被称为"平衡串",问你

【CF】873B 前缀和+map

Balanced Substring 刚讲过差分与前缀和专题,一直以为这两个名词很高大上,其实也就那回事.哈哈. 题源:https://codeforces.com/contest/873/problem/B 题意:给你一串01字符串,让你寻找其中最长的平衡字符串长度. 平衡字符串:字符串中 1的个数=0的个数 题解:所谓平衡,那么当遇见0的时候当-1.然后利用前缀和的思想,求出前缀和. 当一个前缀和第二次出现的时候就是出现了平衡字符串.那么我们用map<int,int>来记录前缀和sum和第

1221. Split a String in Balanced Strings

Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced string s split it in the maximum amount of balanced strings. Return the maximum amount of splitted balanced strings. Example 1: Input: s = "RLRRLLRLRL"

indesOf.substr,substring,charAt的区别

var a = "asdfghjkl" alert(a.substr(1, 3));        //  从下标为1开始,往右数3个长度的数, 显示 sdf; alert(a.substring(1, 3));   //  从下标为1开始,到下标为3结束(左闭右开), 显示 sd; alert(a.indexOf("s", 1));   //  找"s",从下标为1开始找,找到显示下标,找不到显示-1, 显示 1: alert(a.charAt

Longest Substring Without Repeating Characters

最长无重复字符的子串 第一次提交:没有考虑字符串为空的情形.错误. 第二次提交:AC 思路: 1.判断字符串是否为空,若非空,进行下一步: 2.定义一个 和字符串等长的整形数组 result[] 和一个 字符数组. 整形数组 用于存放从每个字符开始计算 无重复字符子串的长度,字符数组用于存放字符串(getChars方法) 3.双重循环.外层循环 len 次,内层循环每次计算result[i] 的值.判断当前字符是否  在开始字符到当前字符之前的一个字符中间的字符串中出现过,若未出现过,resul

3 Longest Substring Without Repeating Characters

1 public int lengthOfLongestSubstring(String s) { 2 long length = s.length(); 3 String tmp = ""; 4 int substringLength = 0; 5 for (int i = 0; i < length; i ++) { 6 if (tmp.contains(("" + s.charAt(i)))) { 7 if (tmp.length() > subs