time limit per test1 second

memory limit per test256 megabytes

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 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

inputCopy

8

11010111

outputCopy

4

inputCopy

3

111

outputCopy

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.

题目就是要你找出最长的0的数量和1的数量相等的长度是多少

唉,一开始觉得2分能做,还做了半天,后面发现有问题有找不出答案,后面一看结果太简单了

把0当成-1,建一个结构体,存前缀和,并保持当前的位置的序号,然后sort,前缀和相同的位置序号相减,保留最大值就好了,第0个为0,0,否则有问题

#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<stdio.h>
#include<float.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define sf scanf
#define pf printf
#define fi first
#define se second
#define mp make_pair
#define pii pair<int,int>
#define scf(x) scanf("%d",&x)
#define prf(x) printf("%d\n",x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define mm(x,b) memset((x),(b),sizeof(x))
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
typedef long long ll;
const ll mod=1e9+7;
using namespace std; const double eps=1e-8;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int N=2e5+10;
char a[N];
struct node{
int num;
int pos;
}q[N];
bool cmp(node x,node y)
{
if(x.num == y.num ) return x.pos < y.pos ;
return x.num <y.num ;
}
int main()
{
int n;scf(n);
sf("%s",a+1); rep(i,0,n+1)
{
if(i==0)
{
q[i].num = 0;
q[i].pos= 0;
}else
{
if(a[i]=='1')
{
q[i].num = q[i-1].num +1;
}else
{
q[i].num = q[i-1].num -1;
}
q[i].pos=i;
}
}
sort(q,q+n+1,cmp);
int ans=0;
rep(i,1,n+1)
{
if(q[i].num == q[i-1].num )
{
int j=i;
while(q[j].num ==q[i-1].num&&j<=n) j++;
j--;
ans=max(ans,q[j].pos -q[i-1].pos);
i=j;
}
}
prf(ans);
return 0;
}

最新文章

  1. 使用Logstash进行日志分析
  2. Protobuf3 + Netty4: 在socket上传输多种类型的protobuf数据
  3. asp.net 站点重启
  4. React入门--------顶层API
  5. UIImage 在某些控件上被放大问题
  6. Hibernate入门4.核心技能
  7. VB6 GDI+ 入门教程[2] GDI+初始化
  8. Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数
  9. YCM安装与配置
  10. dp题目
  11. Redis教程03——Redis 发布/订阅(Pub/Sub)
  12. Linux安装开发环境,必须配置的环节(Fedora15版本)
  13. Java代码整理
  14. python pyfits
  15. [置顶] 强大的JQuery
  16. hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题
  17. mac 下安装securecrt
  18. 掌握numpy(三)
  19. Selenium自动化 Xpath-元素定位
  20. KVM虚拟机配置笔记

热门文章

  1. win10只有edge浏览器能上网的解决方法
  2. E: Unable to locate package clang-7 E: Unable to locate package clang++-7 E: Couldn&#39;t find any package by regex &#39;clang++-7&#39;
  3. adb pull 报错处理:adb: error: cannot create file/directory &#39;E:\&#39;: No such file or directory
  4. Git(2):本地版本库的一些操作
  5. idea的一些设置
  6. SQL语句case when then的用法
  7. js简单实现自动轮播
  8. PDF中的空白页面怎么删除,PDF页面删除技巧
  9. hibernate-第一章-基础
  10. unity3d优化-代码篇(不定期更新)