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’个数相等,则 ...
随机推荐
- 分享几道Java线程面试题
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程 ...
- Apache 2 移植到Arm开发板
第一步,安装pcre: tar -xvzf pcre-8.31.tar.gz cd pcre-8.31 ./configure --prefix=$ARMROOTFS/usr/pcre 的错误,如下图 ...
- Tomcat服务部署步骤
Tomcat服务部署步骤 1. 2. 3. tar -zxvf apache-tomcat-7.0.68.tar.gz,然后修改文件夹名称为需要的名称, 使用mv命令 4. 删除 /webapps/R ...
- spring mvc: 注解和JavaConfig实例
通过javaConfig来配置config,并能正常访问url. 先看图 访问地址:http://localhost:8080/gugua5/ http://localhost:8080/gugua5 ...
- EFCore
Nuget引用 LinqKit.Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.SqlServer 然后新建类继承DbConte ...
- Javascript中的void
原来void是将其后的字面量当元表达式执行,并永远返回undefined.同时undefined不是关键词.. 由于JS表达式偏啰嗦,于是最近便开始采用Coffeescript来减轻负担.举个栗子,当 ...
- flask学习(六):URL传参
1. 参数的作用:可以在相同的URL,但是指定不同的参数,来加载不同的数据 例如:简书上每一篇文章前面的URL相同,只是后面的参数不同 2. 在flask中如何使用参数: 注意: 1) 参数需要放在两 ...
- 1059: [ZJOI2007]矩阵游戏 二分图匹配
https://www.lydsy.com/JudgeOnline/problem.php?id=1059 裸的二分图匹配,行列匹配即可 /****************************** ...
- MVC,MVVM,MVP等设计模式的分析
从Script到Code Blocks.Code Behind到MVC.MVP.MVVM 三个模式按照大致的历史进程将这些概念进行划分: Script Code Blocks.Code Behind ...
- Prism 4 文档 ---第8章 导航
作为同用户具有丰富的交互的客户端应用程序,它的用户界面(UI)将会持续不断的更新来反映用户工作的当前的任务和数据.用户界面可以进行一段时间相当大的变化作为用户交互的应用程序中完成各种任务.通过 ...