B. Balanced Substring
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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 tor - 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字符串,求出现0、1出现次数相等的最长子串的长度。

【分析】:

change array a[]==0 to -1.find prefix[]=prefix sum

find two indices of maximum difference for which it is same value.

take example.

1 0   0 1

1 -1 -1 1

prefix[]=1 0 -1 0

思路:如果某子串中0与1的个数相等那么该子串的代数和肯定等于0。如果最长子串是从开头开始的那么当代数和等于0出现的最后边的位置即最长子串的末尾,该子串即最长子串;如果不是从开头开始的,那么最长子串即代数和相等的两个位置的距离最长的那个子串。用一个数组mp[]来记录加和首次出现的位置,如果该加和还没有出现过就记录下来此时位置,由于是从前往后遍历的,则首次记录的位置肯定是最前边的位置,那么同等加和出现的位置越往后则两者距离就越长。详细请看代码即代码旁的注释。

【代码】:

#include <bits/stdc++.h>

using namespace std;

map<int,int> mp;
int a[+];//前缀和数组
int main()
{
int n;
char c;
cin>>n;
int sum=;
for(int i=;i<=n;i++)
{
scanf(" %c",&c);
if(c=='')
sum++;
else
sum--;
a[i]=sum;//将前缀和装入a数组
if(!mp[sum])//如果此时的代数和之前没出现过则记录下来此时的位置
mp[sum]=i;
}
mp[]=;//前缀和位置数组
int ans=;
for(int i=;i<=n;i++)
ans=max(ans,i-mp[a[i]]);//如果此代数和之前出现过就计算一下当前位置距离首次此代数和出现位置有多远,然后再与ans比较取最大
cout<<ans;
return ;
}

遇见一个1,sum++,遇见0,sum--;记录每一个的sum的位置,,dis[sum]=i; 初始的时候把dis全部初始化为-1,如果当前dis[sum]= -1证明这个sum第一次出现,记录其出现的位置。如果当前did[sum]!=-1,那么证明这个sum,之前出现过,那么从之前那个位置到现在这个位置,中间的和为0,sum+0=sum 证明中间是一部分平衡01串,然后一直更新,最后的结果就是最长的平衡01串。类似组合数学中做鸽巢定理一道题的思路。因为有负数的存在,所以每个sum+1e5。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
char num[]; //存储01数字串
int dis[]; //记录代数和首次出现的位置
int main()
{
//freopen("lalala.text","r",stdin);
while(~scanf("%s",num))
{
memset(dis,-,sizeof(dis)); //初始化为-1
int len=strlen(num);
int sum=,mm=; //sum记录从开头到当前位置的代数和,mm代表此时最长子串的长度
for(int i=; i<len; i++)
{
if(num[i]=='') //如果是1就+1
sum++;
else if(num[i]=='') //如果是0就-1
sum--;
if(sum==) //如果代数和是0就说明该最长子串是从头开始的
{
mm=max(mm,i+); //只要出现0就比一下最长长度
continue;
}
if(dis[sum+]==-) //为了以防出现负数sum+1000000保证下标始终为正;如果此时的代数和之前没出现过则记录下来此时的位置
dis[sum+]=i;
else //如果此代数和之前出现过就计算一下当前位置距离首次此代数和出现位置有多远,然后再与mm比较取最大
{
mm=max(mm,i-dis[sum+]);
}
}
printf("%d\n",mm);
}
return ;
}

http://blog.csdn.net/qq_34131212/article/details/78229621

Educational Codeforces Round 30 B【前缀和+思维/经典原题】的更多相关文章

  1. Educational Codeforces Round 30

    Educational Codeforces Round 30  A. Chores 把最大的换掉 view code #pragma GCC optimize("O3") #pr ...

  2. Educational Codeforces Round 40 C. Matrix Walk( 思维)

    Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...

  3. Educational Codeforces Round 30 D. Merge Sort

    题意:给你n和k,n代表有多少个数,k代表几次操作,求一个1到n的序列,要k次mergesort操作才能还原 Examples Input 3 3 Output 2 1 3 Input 4 1 Out ...

  4. Educational Codeforces Round 30 A[水题/数组排序]

    A. Chores time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  5. Educational Codeforces Round 57D(DP,思维)

    #include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...

  6. Educational Codeforces Round 9 A. Grandma Laura and Apples 水题

    A. Grandma Laura and Apples 题目连接: http://www.codeforces.com/contest/632/problem/A Description Grandm ...

  7. Educational Codeforces Round 22 E. Army Creation(分块好题)

    E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. Educational Codeforces Round 59 (Rated for Div. 2) (前四题)

    A. Digits Sequence Dividing(英文速读) 练习英语速读的题,我还上来昏迷一次....只要长度大于2那么一定可以等于2那么前面大于后面就行其他no 大于2的时候分成前面1个剩下 ...

  9. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

随机推荐

  1. Spark程序

    Spark认识&环境搭建&运行第一个Spark程序 2017-07-09 17:17 by 牛仔裤的夏天, 181 阅读, 0 评论, 收藏, 编辑 摘要:Spark作为新一代大数据计 ...

  2. 有用的Java注解

    好处: 能够读懂别人的代码,特别是框架相关的代码: 让编程更加简洁,代码更加清晰. 使用自定义注解解决问题!! Java1.5版本引入. Java中的常见注解 @Override:告诉使用者及编译器, ...

  3. Conjugate 解题报告

    Conjugate 问题描述 在不存在的 \(\text{noip day3}\) 中,小 \(\text{w}\) 见到了一堆堆的谜题. 比如这题为什么会叫共轭? 他并不知道答案. 有 \(n\) ...

  4. oracle大数据匹配处理C#

    忙碌了几天写出来的oracle存储过程在作业中执行. 写的oracle存储过程如果有什么不好的地方大家指点指点. oracle存储过程其中使用到游标嵌套.if.if嵌套.数据插入表.select插入表 ...

  5. [学习笔记]动态dp

    其实就过了模板. 感觉就是带修改的dp [模板]动态dp 给定一棵n个点的树,点带点权. 有m次操作,每次操作给定x,y表示修改点x的权值为y. 你需要在每次操作之后求出这棵树的最大权独立集的权值大小 ...

  6. 【BZOJ】2453: 维护队列【BZOJ】2120: 数颜色 二分+分块(暴力能A)

    先说正解:把所有相同的数相成一个链在每一个区间里的种数就是不同链的链头,那么记录每个数的上个相同数所在位置,那么只要找出l到r之间前驱值在l之前的数的个数就可以了 本人打的暴力,有一个小技巧,用cha ...

  7. mysql的对象

      mysql 常见的数据对象有哪些: DataBase/Schema Table Index View/Trigger/Function/Procedure   多Database用途: 业务的隔离 ...

  8. MAC地址的介绍(单播、广播、组播、数据收发)

    MAC地址组成 网络设备的MAC地址是全球唯一的.MAC地址长度为48比特,通常用十六进制表示.MAC地址包含两部分:前24比特是组织唯一标识符(OUI,OrganizationallyUniqueI ...

  9. python爬取七星彩的开奖历史记录

    1.因为人不可能一直无休止的学习,偶尔也想做点儿别的,昨天无聊就想写写Python,当然我承认我上班后基本都是在学工作方面的事情,在这个岗位我也呆了三年多了,还是那句话问我什么会不会我会给你说我啥都会 ...

  10. MyBatis查询结果resultType返回值类型详细介绍

    一.返回一般数据类型 比如要根据 id 属性获得数据库中的某个字段值. mapper 接口: // 根据 id 获得数据库中的 username 字段的值 String getEmpNameById( ...