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. List去重问题引出来的hashCode和equals方法

    一.List 里面是基本类型的去重问题 import java.util.ArrayList; import java.util.HashSet; import java.util.List; imp ...

  2. Warning:Configuration 'compile' is obsolete and has been replaced with 'implementation'. It will be

    1.替换 compile为implementation. 2.file->invalidate caches 或者build中的clear

  3. C语言复习: 二级指针和多级指针

    二级指针内存模型建立 void main2() {     int i = 0;       //指针数组     char * p1[] = { "123", "456 ...

  4. How to Pronounce the Word OR

    How to Pronounce the Word OR Share Tweet Share Tagged With: OR Reduction Study the OR reduction.  Th ...

  5. centos 下修改mysql 默认字符集

    解决办法: CentOS 7下修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 mysql  -u root - ...

  6. Cookie-base 认证实现(学习笔记)

    第一步 新建一个ASP.NET core 默认项目 新建 AdminController public class AdminController : Controller { [Authorize] ...

  7. 强制停止ORACLE数据库

    操作环境 SuSE+Oracle11gR2 适用场景 shutdown immediate停止数据库失败 操作命令 1.kill掉oracle实例相关进程 2.清除oracle占用的共享内存段 ipc ...

  8. react native 中es6语法解析

    react native是直接使用es6来编写代码,许多新语法能提高我们的工作效率 解构赋值 var { StyleSheet, Text, View } = React; 这句代码是ES6 中新增的 ...

  9. splunk + docker-compose 实现自定义 index

    splunk是一款非常优秀的运维管理平台.Splunk 是机器数据的引擎.使用 Splunk 可收集.索引和利用所有应用程序.服务器和设备生成的快速移动型计算机数据 . 使用 Splunking 处理 ...

  10. IIS8.0 部署WCF Services

    今天在Win 8的IIS上部署WCF Services,访问SVC文件时出现找不到处理程序的错误,以前遇到这个问题时都是尝试通过注册asp.net的方式处理一下,但是在Win8下这招不灵了,出现如下提 ...