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(思维)的更多相关文章
-
[Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
-
Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
-
Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
-
837B. Balanced Substring
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
-
Balanced Substring
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...
-
【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 ...
-
Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
随机推荐
-
mysql 查询数据时按照A-Z顺序排序返回结果集
mysql 查询数据时按照A-Z顺序排序返回结果集 $sql = "SELECT * , ELT( INTERVAL( CONV( HEX( left( name, 1 ) ) , 16, ...
-
mvcAPI (入门 2)
1)建立一个实体类 using System; using System.Collections.Generic; using System.Linq; using System.Web; names ...
-
理解DOM中的事件流
浏览器发展到第四代时(IE4和Netscape Communicator 4),浏览器团队遇到一个很有意思的问题:页面的哪一部分会拥有特定的事件?想象下在一张纸上有一组同心圆,如果你把手指放在圆心上, ...
-
DRAM与NAND Flash产业六大趋势预测分析
集邦科技(TrendForce)旗下的分析部门DRAMeXchange的研究,针对对DRAM与NANDFlash产业的长久观察下,提出了对2012-2015年间产业发展的六大趋势预测: 趋势一 ...
-
Java、javax、org、sun、Java.util等常用包的区别、详解、实例
Java.javax.org.sun包都是jdk提供的类包,且都是在rt.jar中.rt.jar是JAVA基础类库(java核心框架中很重要的包),包含lang在内的大部分功能,而且rt.jar默认就 ...
-
AnjularJS表单回车提交事件
问题: 输入手机号之后,再验证码输入框点击回车时,触发发送验证码事件. <div class="login-main"> <form name="log ...
-
Spark注册UDF函数,用于DataFrame DSL or SQL
import org.apache.spark.sql.SparkSession import org.apache.spark.sql.functions._ object Test2 { def ...
-
Hexo搭建博客笔记
Hexo搭建(建议看ppt:https://files.cnblogs.com/files/-SANG/%E4%BD%A0%E7%9A%84%E7%8C%AB.pptx ) 安装Git https:/ ...
-
SNAT和DNAT
1.SNAT iptables防火墙 Centos6.6 64位 iptables 内网:eth0 172.16.4.1 外网:eth 112.112.112.112/24 当有用户访问公网时,修改用 ...
-
linux 常用命令-ps命令
ps(process status):进程状态相关命令 1.