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. python 机器学习三剑客 之 Matplotlib

    Matplotlib介绍: Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形 . 通过 Matplotlib,开发者可以仅需要几 ...

  2. thrift安装及python和c++版本调试

    一.安装过程 1.安装依赖库 ]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-opt ...

  3. 20175204 张湲祯 2018-2019-2《Java程序设计》第八周学习总结

    20175204 张湲祯 2018-2019-2<Java程序设计>第八周学习总结 教材学习内容总结 -第十五章泛型与集合框架要点: 一.泛型 1.泛型(Generics)的主要目的是可以 ...

  4. patindex

    功能:返回模式在字符串中第一次出现的位置 解释:patindex('%pattern%',expression) pattern:要查找的模式 expression:被找的字符串 例子:select ...

  5. 通过 iis或者本地IP 调试代码

    首先说下这个操作的意义,做微信开发每次需要将代码部署后才能调试.现在设置了Nginx服务器,生产环境可以指向正式服务器地址,调试时可以将Nginx指向自己的PC,但是vs调试启动的默认地址是[loca ...

  6. linux 安装虚拟机

    如果虚拟机创建不了就重启电脑 重启时 按下F2 出现后 第二个 往下 有个默认的 那个那个 打开虚拟机 选择第一个 然后是选择语言选择软件里面的 软件选择选择 基本网页服务器(右侧选择 python ...

  7. js数据结构与算法——队列

    <script> //创建一个队列 function Queue(){ let items = []; //向队尾添加一个新的项 this.enqueue = function(eleme ...

  8. C#控制台或应用程序中两个多个Main()方法的设置

    大多数初级程序员或学生都认为在C#控制台或应用程序中只能有一个Main()方法.但是事实上是可以有多个Main()方法的. 在C#控制台或应用程序中,在多个类中,且每个类里最多只能存在一个Main() ...

  9. [原创]免固件开发USB2.0 FPGA方案 速度40Mbyte/s+

    USB 2.0接口,实测速度40Mbyte/s: 一个接口实现两种功能(USB2.0+FPGA配置): 免固件开发: 完整的FPGA代码,即拿即用: FPGA逻辑工程师开发USB接口福音: 平台可移植 ...

  10. 【Linux】日志分析工具grep sed sort

    遇到一个问题,在查询日志时发现,服务器上打印的文件有很多个,每个都存储了一部分日志, 需要将日志按照时间排序,并显示所有日志. 原命令: grep -h  searchContent */*log 搜 ...