【Cf edu 30 B. Balanced Substring】
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard 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
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.
【翻译】给出01串,求出最长连续子串使得01个数相同
题解:
     ①经典思想:维护差值——将0设为-1
②前缀和中,前缀和相同的两点可构成合法区间,直接统计最大值就是了
#include<stdio.h>
#include<algorithm>
#define go(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=;
int n,a[N],pos[N],_[N],sum[N],ans;
int main()
{
scanf("%d",&n);
go(i,,n)scanf("%1d",a+i);
go(i,,n)sum[i]=sum[i-]+(a[i]?:-);
go(i,,n)if(pos[sum[i]]||sum[i]==)
ans=max(ans,i-pos[sum[i]]);else pos[sum[i]]=i;
printf("%d\n",ans);return ;
}//Paul_Guderian
.
【Cf edu 30 B. Balanced Substring】的更多相关文章
- 【CF edu 30 D. Merge Sort】
		
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
 - 【CF edu 30 C. Strange Game On Matrix】
		
time limit per test 1 second memory limit per test 256 megabytes input standard input output standa ...
 - 【CF edu 30 A. Chores】
		
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
 - 【CF Round 434 B. Which floor?】
		
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
 - 【CF Edu 28 C. Four Segments】
		
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
 - 【CF Edu 28 A. Curriculum Vitae】
		
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
 - 【CF Edu 28 B. Math Show】
		
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
 - 【2016.3.30项目技术记录】]VS2010自动生成MFC单文档框架程序的修改:去除属性框,在CViewTree类中添加鼠标单击响应
		
转自http://blog.csdn.net/yanfeiouc2009/archive/2010/06/07/5653360.aspx 手头上有个东西要用到单文档,由于想省事,直接用VS2010做了 ...
 - 【2020.11.30提高组模拟】剪辣椒(chilli)
		
剪辣椒(chilli) 题目描述 在花园里劳累了一上午之后,你决定用自己种的干辣椒奖励自己. 你有n个辣椒,这些辣椒用n-1条绳子连接在一起,任意两个辣椒通过用若干个绳子相连,即形成一棵树. 你决定分 ...
 
随机推荐
- python爬虫之有道在线翻译
			
今天初学了python这门课 老师简单的讲解了一下 python的安装环境,配置环境变量,当前主流Python使用的是3.x版本, 下午简单的讲解了python的起源,发展以及在各个方面的应用 然后晚 ...
 - ccpc  2018  final  G - Pastoral Life in Stardew Valley
			
#include <iostream> #include<cstdio> #include<cstring> #include<queue> using ...
 - HBase java API 的使用范例(增,删,查,扫描)
			
编辑pom.xml <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase ...
 - 4.HBASE数据迁移方案(之snapshot):
			
4.HBASE数据迁移方案: 4.1 Import/Export 4.2 distcp 4.3 CopyTable 4.4 snapshot 快照方式迁移(以USER_info:user_lo ...
 - easyui combox 随便不存在的值,清空
			
onHidePanel: function () { var valueField = $(this).combobox("options").valueField; var va ...
 - spring boot 入门3  如何在springboot 上使用AOP
			
Aop是spring的两大核心之一 那么如何在springboot中采用注解的形式实现aop那? 1)首先我们定义一个相关功能的切面类 并 采用@Aspect 注解来声明当前类为切面 同时采用@Com ...
 - MediaTypeListWidget->insertItem 添加的label没有填充单元格
			
label没有填充满当前的item,但是主界面拉伸或者大小变化之后会填充当前的item 类似相关的问题我猜测都是因为子控件或者需要参考的控件的参考对象的大小在初始化的时候还没有完成最终的初始化,导致大 ...
 - 【紫书】(UVa1347)Tour
			
继续考虑dp题目. 题意分析 其实这里只是更加仔细的做一个lrj的复读机(Orz 他分析了一个很重要的结果:如果是一个人从左到右再回来,并且每个点恰经过一次,那么等价于两个人从左到右每个点经过一次地遍 ...
 - 数据库学习(三) sql语句中添加函数 to_char,round,连接符||
			
** to char 是把日期或数字转换为字符串 to date 是把字符串转换为数据库中得日期类型 参考资料:https://www.cnblogs.com/hllnj2008/p/533296 ...
 - POJ 2229 递推
			
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...