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' ...
随机推荐
- day41-解决粘包问题
一.socket缓冲区 研究粘包之前先看看socket缓冲区的问题: 二.socket缓存区的详细解释 每个socket被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区. write()/send ...
- 子类中的成员函数覆盖父类(name hiding)
只要子类中出现了和父类中同名的函数,父类中的所有这个名字的函数,就被屏蔽了. 静态函数成员也是如此?经过代码验证,确实如此. #include <iostream> using names ...
- css:关于position和float
在CSS中,我们是通过定位属性position来进行定位的,具体它有如下几个属性值.常见的属性有如下几个: absolute 生成绝对定位的元素,相对于static定位以外的第一个父元素进行定位.元 ...
- c中计时函数 clock()
#include<time.h> int main() { // ... .. // .... printf("Time used = %.2lf\n",(double ...
- 【原创】锐捷实现OSPF路由协议和NAT地址转换协议
路由网络设计与实施 [锐捷设备实现OSPF路由协议与NAT地址转换] 说明: 本文是在多VLAN双星型交换网络的基础之上发展的.关于组建多VLAN双星型交换网络,请参阅: <思科和锐捷组建多 ...
- Downloadftp
#!/bin/bash FILENAME=$ DSTDIR=$ FTPSRV=ip FTPUSER="user" FTPPWD="password" SRCDI ...
- VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- WilliamChart各种图表效果实现大全《IT蓝豹》
WilliamChart各种图表效果实现大全,有水平线条表格,有柱状表格等.由LineFragment,BarFragment,StackedFragment,SandboxFragment几个fra ...
- WinForm textbox 全选
原地址:忘了 textBox1.KeyPress += anyTextBox_KeyPress; private void anyTextBox_KeyPress(object sender, Sys ...
- python import sklearn出错 "ImportError: DLL load failed: 找不到指定的模块。
安装好sklearn模块后,import的时候出现了以下错误: 但是确实已经装好了,百思不得其解,网上查找之后发现,出现错误原因:安装包的来源问题,也可以理解为包版本兼容问题,有的包使用官方出版,有的 ...