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’个数相等,则 ...
随机推荐
- C#忽略decimal多余的0
decimal test=1.2000:test.ToString("0.####");
- Apache 配置SSL网站
1. 申请证书 现在可以在阿里云或七牛上申请免费的证书,这里以阿里云为例 进入阿里云证书申请界面 https://www.aliyun.com/product/cas ...
- 微信小程序-----安装,编写第一个小程序和运行到手机端
第一步: 微信公众平台注册账号,并选择小程序,网址:mp.weixin.qq.com 填写相关信息,如:主体类型(个人或者企业) AppID 在开发中都是用的到的,服务器域名在网络请求也是用的到的. ...
- p-value值的认识
待续:https://www.zhihu.com/question/21429785/answer/147047565
- 使用C++为对象分配与释放内存时的几个好习惯
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/ 最近在为公司的项目写内存泄漏定位工具,遇到一些关于C++构造与析构对象的问题,在此记录一下 ...
- java中容器的学习与理解
以前一直对于java中容器的概念不理解,虽然学习过,但始终没有认真理解过,这几天老师提出了这样一个问题,你怎么理解java中的容器.瞬间就蒙了.于是各种搜资料学习了一下,下面是我学习后整理出来的的一些 ...
- 十六、dbms_space_admin(提供了局部管理表空间的功能)
1.概述 作用:提供了局部管理表空间的功能 2.包的组成 1).segment_verify作用:用于检查段的区映像是否与位图一致语法:dbms_space_admin.segment_verify( ...
- *SCM-MANAGER独立部署方式
从官网获取最新版本 scm-manager 独立安装包 https://www.scm-manager.org/download/ 解压 为合适的路径 修改 services.bat 文件服务相关信息 ...
- 在CentOS7 安装ffmpeg
参考自:https://linuxize.com/post/how-to-install-ffmpeg-on-centos-7/ 首先切换至root用户 yum install epel-releas ...
- C++面向对象高级编程(一)基础篇
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 概要: 知识点1 构造函数与析构函数 知识点2 参数与返回值 知识点3 const 知识点4 函数重载(要与重写区分开) 知识点5 友元 先以C ...