题目链接:http://codeforces.com/problemset/problem/873/B

题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则称为平衡子串,求最长的平衡子串。

解题思路:将0换成-1,算出每个点的前缀和,若两个点前缀和相同,从第一个点到第二个点之前的字符串则为平衡串,求出最长的即可。

代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=1e5+;
int Min[*N];//Min[i]表示前缀和为i的起始位置 int main(){
memset(Min,0x3f,sizeof(Min));
int n,sum=N,ans=;//sum=N防止下标为负
char ch;
scanf("%d",&n);
getchar();
Min[sum]=;
for(int i=;i<=n;i++){
scanf("%c",&ch);
if(ch=='')
sum++;
else
sum--;
Min[sum]=min(Min[sum],i);
ans=max(ans,i-Min[sum]);
}
printf("%d\n",ans);
return ;
}

Codeforces 873B - Balanced Substring(思维)的更多相关文章

  1. CodeForces - 873B Balanced Substring(思维)

    inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...

  2. [Codeforces 873B]Balanced Substring

    Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s  ...

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

    题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...

  4. 837B. Balanced Substring

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. Balanced Substring

    You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...

  6. 【Cf edu 30 B. Balanced Substring】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. CF873B Balanced Substring (前缀和)

    CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...

  8. 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 ...

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

    You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...

随机推荐

  1. BZOJ2301:[HAOI2011]Problem b——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 https://www.luogu.org/problemnew/show/P2522 对于给 ...

  2. BZOJ1876:[SDOI2009]SuperGCD——C++高精度良心题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1876 Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数 ...

  3. 【单调队列】【P2627】 修剪草坪

    传送门 Wa这次竟然不是Uva的题 Description 在一年前赢得了小镇的最佳草坪比赛后,Farm John变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,Farm John希 ...

  4. 如何修改即时聊天websocket的端口号

    需要修改2个地方 1.修改\src\main\java\com\woodstudio\framework\modules\chat\StartFilter.java里面的端口号   2.修改\src\ ...

  5. HDU1569 最大流(最大点权独立集)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. React Mixin

    为什么使用 Mixin ? React为了将同样的功能添加到多个组件当中,你需要将这些通用的功能包装成一个mixin,然后导入到你的模块中. 可以说,相比继承而已,React更喜欢这种组合的方式. 写 ...

  7. Hibernate入门(3)- 持久对象的生命周期介绍

    在hibernate中对象有三种状态:瞬时态(Transient). 持久态(Persistent).脱管态或游离态(Detached).处于持久态的对象也称为PO(Persistence Objec ...

  8. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  9. 2015/8/26 Python基础(1):基本规则及赋值

    Python有如下的基本规则: #后表示注释 \n是行分隔符 \是继续上一行,将过长语句分开 :分号将两个语句连接在一行中 :冒号将代码头和体分开 代码块用缩进块的方式体现 不同缩进深度分隔不同的代码 ...

  10. MSSQL Procudure Sample

    代码: USE [Internal_Timesheet] GO /****** Object: StoredProcedure [dbo].[ManageTSReminder] Script Date ...