time limit per test 1 second

memory limit per test 256 megabytes

input standard input

output standard 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 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.

Examples

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.

【翻译】给出01串,求出最长连续子串使得01个数相同

题解:
     ①经典思想:维护差值——将0设为-1

     ②前缀和中,前缀和相同的两点可构成合法区间,直接统计最大值就是了

#include<stdio.h>
#include<algorithm>
#define go(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=;
int n,a[N],pos[N],_[N],sum[N],ans;
int main()
{
scanf("%d",&n);
go(i,,n)scanf("%1d",a+i);
go(i,,n)sum[i]=sum[i-]+(a[i]?:-);
go(i,,n)if(pos[sum[i]]||sum[i]==)
ans=max(ans,i-pos[sum[i]]);else pos[sum[i]]=i;
printf("%d\n",ans);return ;
}//Paul_Guderian

.

【Cf edu 30 B. Balanced Substring】的更多相关文章

  1. 【CF edu 30 D. Merge Sort】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. 【CF edu 30 C. Strange Game On Matrix】

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

  3. 【CF edu 30 A. Chores】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. 【CF Round 434 B. Which floor?】

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

  5. 【CF Edu 28 C. Four Segments】

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

  6. 【CF Edu 28 A. Curriculum Vitae】

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

  7. 【CF Edu 28 B. Math Show】

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

  8. 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应

    转自http://blog.csdn.net/yanfeiouc2009/archive/2010/06/07/5653360.aspx 手头上有个东西要用到单文档,由于想省事,直接用VS2010做了 ...

  9. 【2020.11.30提高组模拟】剪辣椒(chilli)

    剪辣椒(chilli) 题目描述 在花园里劳累了一上午之后,你决定用自己种的干辣椒奖励自己. 你有n个辣椒,这些辣椒用n-1条绳子连接在一起,任意两个辣椒通过用若干个绳子相连,即形成一棵树. 你决定分 ...

随机推荐

  1. ElasticSearch : 基础

    #新建索引以及类型: PUT http://10.18.43.3:9200/test { "settings": { "number_of_shards": 3 ...

  2. ECSHOP和SHOPEX快递单号查询韵达插件V8.6专版

    发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...

  3. python计算MD5

    python有自带的MD5模块hashlib,用起来简单很多.Python Hashlib模块的使用说明 http://docs.python.org/2/library/hashlib.htmlfd ...

  4. weui-switch开关控件,表单提交后如何取值

    最近在学习weui这个框架,做了一些小的试验,发现weui-switch控件直接提交不能获取到表单信息,在segmentfault上发现也有人提了这个问题,有人说可以设置一个隐含标签来捕获开关的状态, ...

  5. zookeeper环境搭建(Linux)

    安装zookeeper 安装jdk(此处省略) 解压tar包并配置变量环境 配置文件修改 将/usr/local/src/zookeeper-3.4.5/conf这个路径下的zoo_sample.cf ...

  6. mysql5.7数据库与5.7之前版本比较

    数据库初始化方式变更 <5.7 版本 mysql_install_db >5.7 版本 bin/mysqld --initialize --user =mysql --basedir=/u ...

  7. css常用样式属性详细介绍

    对于初学css的来说,肯定会觉得这么多样式不好记,而且记住了也容易忘,其实刚开始我们不用去记这么多的样式,确实是记了也会忘,刚开始只需记住一些常用的就可以了,然后在慢慢的使用过程当中接触并学习一些高级 ...

  8. MFC随笔记录——1

    这段时间用MFC做完了项目里的一个对图像处理(字迹匹配)的软件,通过项目的具体要求的一步一步的实现,我也学习到了很多以前困惑很久的问题,算是对自己的一个提高吧,把一些有技巧性的操作记在这里,给以后的自 ...

  9. 给socks-proxy-agent增加认证

    由于需要使用socks代理,查看了nodejs的各种socks库,最终的结论是socks库是其中最完善的,而socks-proxy-agent是以其为基础的封装,可以直接和http模块对接. 不过在尝 ...

  10. TortoiseSVN的安装使用

    下面分享一篇关于TortoiseSVN的安装以及使用 1.运行TortoiseSVN-1.6.6.17493-win32-svn-1.6.6.msi程序, 开始安装 2.点击Next, 下一步 3.选 ...