Balanced Substring

CodeForces - 873B

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.

题目大意:字符串是由0和1组成,求字符串的最长子串,使0和1的数目相同

思路:维护一个前缀和,初始化sum为n,如果是0,--,如果是1就++,如果这个sum值之前出现过了,那么说明最早出现这个值的位置和现在位置的长度就是当前构成01相等字符串的长度,每次更新ans。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
int Min[200005];//Min[sum]表示前缀和sum的最早出现位置
char s[100005];
int main(){int n,sum,len,ans;while(scanf("%d",&n) != EOF){scanf("%s",s+1);sum = n;//防止sum为负数导致Min数组越界初始化为nans = 0;len = strlen(s+1);memset(Min,INF,sizeof(Min));Min[sum] = 0;//初始Min[n]=0,如果整个串01个数相同就是原长度for(int i = 1; i <= len; i++){if(s[i] == '0') sum--;else sum++;Min[sum] = min(Min[sum],i);ans = max(ans,i - Min[sum]);}printf("%d\n",ans);}return 0;
}

Balanced Substring CodeForces - 873B (思维+前缀和)相关推荐

  1. codeforces Balanced Substring

    B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. A. Balanced Substring

    题目如下: You are given a string s, consisting of n letters, each letter is either 'a' or 'b'. The lette ...

  3. Balanced Substring

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

  4. Codeforces Round #632 (Div. 2) C. Eugene and an array 思维 + 前缀和

    传送门 文章目录 题意: 思路: 题意: 给定一个长度为nnn的序列aaa,定义一段区间为好区间是这段区间的所有连续子区间的和都不为000,求好区间的个数. 思路: 套路题,定义aia_iai​的前缀 ...

  5. CodeForces-1016C Vasya And The Mushrooms(模拟+思维+前缀和的前缀和) 解题报告 Apare_xzc

    CodeForces-1016C Vasya And The Mushrooms(模拟+思维+二重前缀和 ) 解题报告 xzc 2019/4/7 这周周赛的C题:wyt学姐的恶意   这道题周赛的时候 ...

  6. 575 div3RGB Substring (hard version)——思维-

    [题目描述] The only difference between easy and hard versions is the size of the input. You are given a ...

  7. CodeForces - 948C(前缀和 + 二分)

    链接:CodeForces - 948C 题意:N天,每天生产一堆雪体积 V[i] ,每天每堆雪融化 T[i],问每天融化了多少雪. 题解:对 T 求前缀和,求每一堆雪能熬过多少天,再记录一下多余的就 ...

  8. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  9. C. k-Amazing Numbers(思维前缀最小值+枚举相同数距离)

    https://codeforces.com/contest/1417/problem/C 大晚上的想岔了.. a1,a2,-,an (1≤ai≤n)这个范围其实就在暗示要枚举出现的数.但是我不知道怎 ...

最新文章

  1. 记一次Python与C#的AES加密对接
  2. c语言 get post请求,URL GET/POST请求目标-c
  3. 一周刷题记录 | WebMisc
  4. pyplot绘制图片_matplotlib系列之pyplot
  5. 【转】全了!临港四镇最新对口地段小学,中学都在这里,看看你的孩子能读哪个学校
  6. python中僵尸进程
  7. ExecuteNonQuery()的返回值
  8. VMware下Hadoop 2.4.1完全分布式集群平台安装与设置
  9. C/C++[sort( )]排序
  10. JSP与Servlet 程序设计教程
  11. java的的socket编程_javasocket编程
  12. fscanf()函数详解
  13. Ken Thompson 的经典 C 程序
  14. 【CF643D】Bearish Fanpages(set)(模拟)
  15. 加密币Terra创始人权道亨已从新加坡飞往杜拜转往第三国
  16. 用计算机弹我们不一样谱子,我们不一样钢琴谱
  17. [转]:职场不得不知的4大潜规则
  18. js原生常用知识点总结
  19. Unity 3d飞机游戏制作1
  20. 小行星大小计算机,一颗有着吉萨大金字塔尺寸的小行星今天掠过地球

热门文章

  1. ★不容错过的PPT教程!
  2. 电话号码验证区号-号码-分机
  3. 核密度估计 Kernel Density Estimation(KDE)
  4. 登录wegame无法定位程序输入点
  5. 云服务器和本地服务器的区别在哪?这些区别令人心动
  6. 2012网络订火车票必胜秘籍(登陆成功率极高)
  7. HTML锚点定位偏移
  8. 大数据是阿猫阿狗都能玩的吗
  9. 【LVI-SAM论文全文翻译】: LVI-SAM: Tightly-coupled Lidar-Visual-Inertial Odometry via Smoothing and Mapping
  10. Redis Zrem 命令