Balanced Substring
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.
Example
8
11010111
4
3
111
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.
求前缀和,然后map一下,前缀和相同的表示两位置之间的字符串达到了平衡。
代码:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int s[];
int main()
{
int n;
string a;
cin>>n;
cin>>a;
map<int,int>q;
int sum = ,maxi = ;
for(int i = ;i < n;i ++)
{
if(a[i] == '')sum += -;
else sum += ;
if(!q[sum]&&sum)q[sum] = i + ;//如果sum为0表示本来就平衡不需要标记了
else
{
maxi = max(maxi,i - q[sum] + );
}
}
cout<<maxi;
}
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 ...
- 837B. Balanced Substring
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- 【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 ...
- CF873B Balanced Substring
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...
- Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
随机推荐
- SPSS t 检验
在针对连续变量的统计推断方法中,最常用的是 t 检验和方差分析两种. t 检验,又称 student t 检验,主要用于样本含量较小(例如n<30),总体标准差未知的正态分布资料.它是用 t 分 ...
- python脚本7_打印九九乘法表
#打印九九乘法表 for i in range(1,10): s = "" for j in range(1,i+1): s += str(j) + '*' + str(i) + ...
- 1-15-2-RAID5 企业级RAID磁盘阵列的搭建(RAID1、RAID5、RAID10)
RAID5的搭建 第一步:添加四个磁盘,开机并检查(略过) 第二步:使用fdisk命令分别对四个磁盘进行分区,效果如下图: 第三步:使用mdadm命令创建RAID5磁盘阵列 [root@localho ...
- [less]用webstorm自动编译less产出css和sourcemap
css产出sourcemap有什么用呢,可能大家要问这个问题了. 请移步这里 https://developers.google.com/chrome-developer-tools/docs/css ...
- 网络编程之socketserver初识
网络编程之socketserver初识 Server #!/usr/bin/env python # @Author : "Wjl" # @Date : 2017/12/22 # ...
- linux 字符串查找
获取指定目录文件名包含指定字符的文件,然后遍历是否有包含特定字符串,有的话打出文件名 #!/bin/sh COMMAND=`find /data/home/ftp/data/20/201704/27/ ...
- DNS智能解析的搭建与配置
分类: LINUX 原文地址:DNS智能解析的搭建与配置 作者:十年梦生 9月份整整忙了一个月,都抽不出时间来写篇文章,这几天趁着10.1终于有时间来写些东西了,将9月份所做的一些东西来做下总结. ...
- 如何让history显示时间
linux和unix上都提供了history命令,可以查询以前执行的命令历史记录但是,这个记录并不包含时间项目因此只能看到命令,但是不知道什么时间执行的如何让history记录时间呢? 解决方案 注意 ...
- Viewpager+Fragmnet懒汉式注意
1.new的时候不会触发setUserVisibleHint(): 2.setUserVisibleHint()在ViewPager中当前缓冲页跳转,可见到不可见或者不可见到可见的时候触发: 3.ge ...
- zoj 2966 Build The Electric System(最小生成树)
Build The Electric System Time Limit: 2 Seconds Memory Limit: 65536 KB In last winter, there wa ...