CodeForces - 873B Balanced Substring(思维)
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' ...
随机推荐
- jquery左侧菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- es6 初级之---const 和 默认参数
1. const 的定义: 1.1 常量定义的时候要赋值,不赋值是会报错的: <!DOCTYPE html> <html lang="en"> <he ...
- jquery接触初级-----juqery 动画函数
1. window.onload(), 一次只能保存对一个函数的引用:如果多次调用,他会自动用后面的函数覆盖前面的函数 2.$(document).ready(); 会在现有行为上追加新的行为,这些函 ...
- Java读写hdfs上的avro文件
1.通过Java往hdfs写avro文件 import java.io.File; import java.io.IOException; import java.io.OutputStream; i ...
- unity WegGL 调用js
test.jslib文件,必须放到Assets/Plugins下,这里是:Assets/Plugins/WebGL mergeInto(LibraryManager.library, { Hello: ...
- ReactiveX 学习笔记(18)使用 RxJS + Angular 调用 REST API
JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...
- Haskell语言学习笔记(72)Free Monad
安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...
- RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World(转载)
RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World 一.简介 RabbitMQ是一个消息的代理器,用于接收和发送消息,你可以这样想,他就是一个邮局,当您把需要寄送的邮件投递到 ...
- [PC]PHPCMS v9.5.6整合UEditer1.4.2
----------------------------------------------------------------------------------------------- 首先去U ...
- [转载]FMS Dev Guide学习笔记(验证用户)
一.开发交互式的媒体应用程序 1.使用外部资源验证用户 对于有限数量的客户,请求用户名密码,然后通过外部资源(像数据库.LDAP服务或其它访问授权服务)验证它们,是可行的. a.SWF在请求连 ...