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

837B. 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. CodeForces - 873B Balanced Substring(思维)

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

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

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

  4. Balanced Substring

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

  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. css3写出飘雪花特效

    大冬天的,飘雪花的特效,你可能要用上了吧.通常情况下用jQuery写飘雪花的特效,但用css3写,其实特别简单,新手一看就懂,那就告别jQuery,用css3轻松搞定飘雪花特效吧! 点击查看特效演示 ...

  2. 让你的Spring Boot应用快速运行在Docker上面

    前置条件: 1. 服务器(我这边是CentOS7)上已经安装了Docker(安装步骤很简单,可以参考我上一篇博客) 2.服务器上已经安装了Java和Maven 在满足以上条件后,我们就可以开始了: 1 ...

  3. 十个推荐使用的 Laravel 的辅助函数

    壹. array_dot() array_dot () 辅助函数允许你将多维数组转换为使用点符号的一维数组. $array = [ 'user' => ['username' => 'so ...

  4. 《Linux就该这么学》 - 必读的红帽系统与红帽linux认证自学手册

    <Linux就该这么学>   本书作者刘遄从事于linux运维技术行业,较早时因兴趣的驱使接触到了Linux系统并开始学习. 已在2012年考下红帽工程师RHCE_6,今年又分别考下RHC ...

  5. nginx配置文件,做多个项目代理

    web01: server { listen 9988; server_name www.oldboy.com; access_log logs/www.oldboy.com_access.log m ...

  6. 1、js的基本对象和垃圾回收

    js常用的基本类型:Undefined,null,string,number,boolen 还有一种复杂的数据类型 object.判断类型可以用 typeof. 确定值是否是有穷的,isFinite, ...

  7. Tools:Android studio 使用

    1. Android Gradle 更新 问题报错 Could not find com.android.tools.build:gradle:2.14.1. 需要更新gradle插件 相关博客 ht ...

  8. Javal连接字符串为Json

    public static String concatJson(String[] keys,String[] values,String[] alreadyJsonKeys){ if(keys==nu ...

  9. 分析python日志重复输出问题

    问题起源: ​ 在学习了python的函数式编程后,又接触到了logging这样一个强大的日志模块.为了减少重复代码,应该不少同学和我一样便迫不及待的写了一个自己的日志函数,比如下面这样: impor ...

  10. 今日报错Cannot access java.lang.String

    public java.long.Long getId() { return id; } public void setId(java.lang.Long id) { this.id = id;} 手 ...