inputstandard input
outputstandard 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串,让你找到最大的[l,r]使得0和1的数量相等

题解

我们知道01数量想等,那么和肯定为0,然后我们把0变成-1

题目就转变成求最长子序列使[l,r]总和为0

代码

 #include<bits/stdc++.h>
using namespace std; const int N=1e5+;
int n,sum,ans;
char s[N];
unordered_map<int,int>S;
int main()
{
scanf("%d%s",&n,s+);S[]=;
for(int i=;i<=n;i++)
{
sum+=(s[i]==''?:-);
if(S.count(sum))ans=max(ans,i-S[sum]);
else S[sum]=i;
}
printf("%d",ans);
return ;
}

CodeForces - 873B Balanced Substring(思维)的更多相关文章

  1. [Codeforces 873B]Balanced Substring

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

  2. Codeforces 873B - Balanced Substring(思维)

    题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...

  3. Codeforces 873 B. Balanced Substring(前缀和 思维)

    题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...

  4. 837B. Balanced Substring

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. Balanced Substring

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

  6. 【Cf edu 30 B. Balanced Substring】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. CF873B Balanced Substring (前缀和)

    CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...

  8. 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 ...

  9. Balanced Ternary String CodeForces - 1102D (贪心+思维)

    You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...

随机推荐

  1. django 认证模块auth,表单组件form

    django认证系统(auth): 1.首先我们在新窗口中打开一个django项目,之后点击,

  2. pod引用第三方库的几种方式

    pod引用库的原理,本质上是去找.podspec文件,podspec中包含库的地址及最新的版本号(tag标签),如果pod时没有指定版本,则pod install时会去下载podspec文件中指定的最 ...

  3. spring获取配制文件的参数

    项目中需要获取一些万年不变的参数,比如单点登录的域名 怎么从多个文件配置中获取呢,原来spring早已经提供了类PropertyPlaceholderConfigurer <?xml versi ...

  4. The value for the useBean class attribute xxx is invalid

    JSP页面报这个错可能的原因: 1:指定的 Bean 类没找到 2:该类不是 public 的,或者找到的 class 文件是 interface 或抽象类 3:Bean 类中没有 public 的无 ...

  5. java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor

    Hibernate4.x与spring3.x整合,有关事务的处理,用Junit4测试,出现org.springframework.beans.factory.BeanCreationException ...

  6. Linux tr命令使用方法

    tr命令主要用于删除文件中控制字符或进行字符转换.本文主要介绍tr命令的基本语法和使用实例. tr基本语法 tr命令格式:tr [ -d ] [ -c ] [ -s ] [ 字符串1 ] [ 字符串2 ...

  7. Structs复习 Structs标签

    如果类型是object Structs会把它默认解析为OGNL表达式 想取字符串的话就 ‘’ ‘ ’ ‘’ jar包 web.xml. <?xml version="1.0" ...

  8. Hibernate学习笔记1.2(Annotation版本的Helloworld)

    hibernate 3.0之后开始支持Annotation 接着1.1的项目 首先 需要创建model Teacher.java. package com.hw.hibernate.model; pu ...

  9. AnimCheckBox按钮点击动画效果《IT蓝豹》

    AnimCheckBox按钮点击动画效果 AnimCheckBox按钮点击动画效果,点击选中后勾选框选择效果,很不错的动画功能.项目来源:https://github.com/lguipeng/Ani ...

  10. eclipse git 冲突管理