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. 关于使用array_rand随机取出数组的值

    代码如下 <?php echo "<meta charset='utf-8'/>";//选择解码方式,防止乱码现象 $a = array("abc&qu ...

  2. 商城项目:商品列表ajax加载,ajax加入购物车--五张表的联合查询

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProductLists.a ...

  3. Apache Struts最新漏洞 远程代码执行漏洞预警 2018年11月08日

    2018年11月8日,SINE安全监控检测中心,检测到Apache Struts官方更新了一个Struts漏洞补丁,这个漏洞是Apache Struts目前最新的漏洞,影响范围较广,低于Apache ...

  4. Mongoose模式的扩展

    模式的扩展 默认值 默认值的类型: 固定值.即使生成 代码展示: var mongoose = require('mongoose');mongoose.connect('mongodb://loca ...

  5. (数据科学学习手札33)基于Python的网络数据采集实战(1)

    一.简介 前面两篇文章我们围绕利用Python进行网络数据采集铺垫了很多内容,但光说不练是不行的,于是乎,本篇就将基于笔者最近的一项数据需求进行一次网络数据采集的实战: 二.网易财经股票数据爬虫实战 ...

  6. (数据科学学习手札20)主成分分析原理推导&Python自编函数实现

    主成分分析(principal component analysis,简称PCA)是一种经典且简单的机器学习算法,其主要目的是用较少的变量去解释原来资料中的大部分变异,期望能将现有的众多相关性很高的变 ...

  7. kafka集群部署文档(转载)

    原文链接:http://www.cnblogs.com/luotianshuai/p/5206662.html Kafka初识 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候 ...

  8. Spring MVC 开发 配置

    1.首先在web.xml中配置一个DispatcherServlet,并通过<servlet-mapping>指定需要拦截的url. 下面xml中配置一个拦截.html为后缀的url. & ...

  9. C++怎么用二维数组作为形参传入

    原文地址:http://blog.csdn.net/xuleicsu/article/details/919801 如何将二维数组作为函数的参数传递 今天写程序的时候要用到二维数组作参数传给一个函数, ...

  10. Linux-OpenSUSE折腾-1(Qt安装,Chrome安装)

    先上图,大蜥蜴还是不错的,偶然看到了大蜥蜴这个系统,我就觉得又可以折腾几天了,先上图 OpenSUSE有一个入门介绍的网站写的相当不错,感兴趣的可以连接过去:https://lug.ustc.edu. ...