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
8
11010111
Output
4
Input
3
111
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[];
int main()
{
int n;
string a;
cin>>n;
cin>>a;
map<int,int>q;
int sum = ,maxi = ;
for(int i = ;i < n;i ++)
{
if(a[i] == '')sum += -;
else sum += ;
if(!q[sum]&&sum)q[sum] = i + ;//如果sum为0表示本来就平衡不需要标记了
else
{
maxi = max(maxi,i - q[sum] + );
}
}
cout<<maxi;
}

Balanced Substring的更多相关文章

  1. [Codeforces 873B]Balanced Substring

    Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s  ...

  2. 837B. Balanced Substring

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. CodeForces - 873B Balanced Substring(思维)

    inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...

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

    题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...

  5. 【Cf edu 30 B. Balanced Substring】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  6. CF873B Balanced Substring (前缀和)

    CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...

  7. codefroces 873 B. Balanced Substring && X73(前缀和思想)

    B. Balanced Substring You are given a string s consisting only of characters 0 and 1. A substring [l ...

  8. CF873B Balanced Substring

    1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...

  9. Codeforces 873B - Balanced Substring(思维)

    题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...

随机推荐

  1. Elasticsearch 使用技巧笔记

    1.重新分片 当出现Unassigned分片时,我们可以通过分片重分配解决这个问题 curl -X PUT http://192.168.0.37:9200/_cluster/settings \ - ...

  2. awardRotate.js

    (function($) { var supportedCSS,styles=document.getElementsByTagName("head")[0].style,toCh ...

  3. xshell5使用ssh连接阿里云服务器

    这里有两种方式,一种是在阿里云的控制台里面进行,另一种是在Xshell里面生成密钥. 阿里云控制台密钥对 点击右上方的创建密钥对 在阿里云里面生成较为简单,点击该页面右上方的“创建密钥对”,在另一个页 ...

  4. 谈一谈最近关闭的Kindle人论坛

    最近Kindle圈子内最大的论坛“Kindle人”关闭了,倒也掀起了一阵小波澜.Kindle人论坛是K友圈子里比较著名的一个“Kindle资源分享论坛”,这么一说其实混了这么久网络,大家都知道这个论坛 ...

  5. 设计模式--观察者模式C++实现

    观察者模式C++实现 1定义 Observer/Publish/subscribe发布订阅模式 定义对象间一种一对多的依赖关系,使得当一个对象改变状态时,所有依赖他的对象都能获得通知并被自动更新 2类 ...

  6. dva-loading使用方法

    1. 下载dva-loading npm install dva-loading --save 2. 导入并使用dva-loading import createLoading from 'dva-l ...

  7. BZOJ 1003 [ZJOI2006]物流运输trans ★(Dijkstra + DP)

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1003 思路 先Dijkstra暴力求出i..j天内不变换路线的最少花费,然后dp[i] = ...

  8. log4j 日志详解

    # 以下是rootLogger的配置,子类默认继承,但是子类重写下面配置=rootLogger+自己配置,我晕#输出到控制台 log4j.appender.console=org.apache.log ...

  9. ElasticSearch6.0 索引模板

    我们在做es搜索的场合,每次创建索引的时候,都需要为每个索引设置mapping的字段映射,现在我们可以为通用的索引创建一个模板 每次创建索引时候,如果匹配到相应的模板 索引的mapping会被自动设置 ...

  10. 在关于原生js获取getAttribute如果是src的一点注意事项

    最近抽空把seajs看完了 他们在fix代码的时候,写明在某浏览器下的情况和官方文档出处,这样有据可查.太赞了 顺便把我想要这段摘出来 可以直接dom.src,但是在ie6-7中是不支持的,并且在ge ...