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

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int N=1e5+5;
 5 int n,sum,ans;
 6 char s[N];
 7 unordered_map<int,int>S;
 8 int main()
 9 {
10     scanf("%d%s",&n,s+1);S[0]=0;
11     for(int i=1;i<=n;i++)
12     {
13         sum+=(s[i]==‘1‘?1:-1);
14         if(S.count(sum))ans=max(ans,i-S[sum]);
15         else S[sum]=i;
16     }
17     printf("%d",ans);
18     return 0;
19 }

原文地址:https://www.cnblogs.com/taozi1115402474/p/9420746.html

时间: 2024-08-30 17:55:45

CodeForces - 873B Balanced Substring(思维)的相关文章

Codeforces 873 B. Balanced Substring(前缀和 思维)

题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置.因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度.但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0.我就是在这个地方错了,这里给出错地方的数据. 1 #include<bi

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 t

CF873B Balanced Substring (前缀和)

CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] - \sum[l][0] = \sum[i][1] - \sum[l][1]\) \(\sum[i][0] - \sum[l][1] = \sum[i][0] - \sum[l][0]\) 然后hash一下DP即可. #include <iostream> #include <cstdio

Balanced Ternary String CodeForces - 1102D (贪心+思维)

You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain

CodeForces 1102C-简单的思维题

题目链接:http://codeforces.com/problemset/problem/1102/C C. Doors Breaking and Repairing time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are policeman and you are playing a game with Slavik

CodeForces 789D 欧拉路径计数,思维

CodeForces 789D 题意:n个点m条边的无向图,求经过其中m-2条边两次,剩下2条边一次的方案数有几种,如果剩下两条边的集合一样算同一种. tags: 选出两条边,其它m-2条边假想复制成两条,这样就是要求欧拉路径是否存在,即奇点个数是否为0或2. 所以该怎么选这两条边呢? 先把边分为自环边和普通边. 1.选取两条不相邻普通边,图中存在4个奇点,不满足欧拉路径条件: 2.选取两条相邻普通边,图中存在2个奇点,满足欧拉路径条件: 3.选取一条普通边一条自环,图中存在2个奇点,满足欧拉路

CodeForces 789E bfs建模,思维

CodeForces 789E 题意:有k种可乐,每种的测试为ai/1000. 要你合成一种浓度为n/1000的可乐,问最小要杯可乐,每种可乐可重复取. tags:  要注意到浓度绝不会超过1000/1000. 假设选取m杯可乐,则 (a1+a2+......+am) / m = n,变换一下为(a1-n)+(a2-n)+......+(am-n) = 0.即要选 m杯可乐,其浓度减 n之和为0.而浓度不超过1000,故(a1-n)+(a2-n)+....+(as-n)的和肯定在 -1000~1

2017-03-16 Codeforces 453A 概率期望,思维 UOJ 228(待补)

Codeforces 453A   A. Little Pony and Expected Maximum 题意:一个m面质地均匀的骰子,每面出现的概率都是独立的1/m, 你需要投掷n次,其结果是这n次出现的最大点数.问投掷n次骰子的结果的期望值是多少,要求相对误差或绝对误差不超过1e-4. tags:枚举骰子出现最大值i,计算出最大值为i时的概率,就得到了答案. 最大值为i的概率=(i/m)^n-((i-1)/m)^n. #include<bits/stdc++.h> using names

Codeforces Round #399 B 思维 C 模拟 D 概率dp E SG博弈

Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)B. Code For 1 题意:数n,不断拆分为 n/2, n&1, n/2,直到都为0或1.求区间[l, r]有多少个1. tags:画一画很容易看出来,类似dfs中序遍历. //#399 B #include<bits/stdc++.h> using namespace std; #pragma comment(linker, &quo