Balanced Substring
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.
Example
8
11010111
4
3
111
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.
求前缀和,然后map一下,前缀和相同的表示两位置之间的字符串达到了平衡。
代码:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int s[];
int main()
{
int n;
string a;
cin>>n;
cin>>a;
map<int,int>q;
int sum = ,maxi = ;
for(int i = ;i < n;i ++)
{
if(a[i] == '')sum += -;
else sum += ;
if(!q[sum]&&sum)q[sum] = i + ;//如果sum为0表示本来就平衡不需要标记了
else
{
maxi = max(maxi,i - q[sum] + );
}
}
cout<<maxi;
}
Balanced Substring的更多相关文章
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- 837B. Balanced Substring
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- 【Cf edu 30 B. Balanced Substring】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CF873B Balanced Substring (前缀和)
CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...
- 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 ...
- CF873B Balanced Substring
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...
- Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
随机推荐
- mysql中索引利用情况(explain用法)
使用explain查看,如下 1.首先创建表test,语句如下 create table test(a int,b varchar(10),c varchar(10)); 2.在表中的a,b都创建索引 ...
- JSP 表达式语言
JSP 表达式语言 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建算术表达式也可以用来创建逻辑表达式.在JSP EL表达式内可以使用整型数,浮点 ...
- android 命令行签名apk文件
签名apk 1.将apk格式改为zip格式包,然后删除原来apk里面的META-INF文件夹,之后改回apk文件格式 2.cmd命令行: jarsigner -verbose -keystore C: ...
- 二十二 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别
第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://github.com/muchrooms/zheye 注意:此程序依赖以下模块包 Keras==2.0.1 Pillow= ...
- proxy-target-class 作用
该属性值默认为false,表示使用JDK动态代理织入增强;当值为true时,表示使用CGLib动态代理织入增强;但是,即使设置为false,如果目标类没有生命接口, 则Spring将自动使用CGLib ...
- poj3308 Paratroopers 最大流 最小点权覆盖
题意:有一个n*m的矩阵,告诉了在每一行或者每一列安装大炮的代价,每一个大炮可以瞬间消灭这一行或者这一列的所有敌人,然后告诉了敌人可能出现的L个坐标位置,问如何安置大炮,使花费最小.如果一个敌人位于第 ...
- RedHat/CentOS 7通过nmcli命令管理网络教程
Red Hat Enterprise Linux 7 和CentOS 7 的网络管理实际上是对NetworkManager的管理,可通过nmcli命令进行控制,下面小编就给大家介绍下RedHat/Ce ...
- 【Raspberry Pi】 小问题汇总
注: 此系列为自己之前所搭建网站内容. 目前入手树莓派2,将遇到的一些琐碎的问题记录在此. 1. 更改时区 查看日期命令:date 输入sudo dpkg-reconfigure tzdata后按提示 ...
- dump相关
命令:jmap -dump:format=b,file=/tmp/dump.hprof pid jstack -l 30087 >> text.txt
- springmvc日期格式化
jsp页面String类型转Controller后台Date类型 方法1.在实体中加入日期格式化注解 @DateTimeFormat(pattern="yyyy-MM-dd") p ...