题目链接: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. BZOJ3673 & BZOJ3674 & 洛谷3402:可持久化并查集——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3673 https://www.lydsy.com/JudgeOnline/problem.php? ...

  2. jq的$.each遍历数组

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 复习java数据库操作的总结

    以前学习java数据库操作,学得那叫糊里糊涂,各种JDBC常用的类和接口根本是傻傻分不清啥是干嘛的.只是套着用用吧. 不过这次好歹清楚些了,呜呜,学习有阶段性,多次重复才有好效果,多么痛的领悟. 工程 ...

  4. bzoj4810 [Ynoi2017]由乃的玉米田 bitset优化+暴力+莫队

    [Ynoi2017]由乃的玉米田 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 917  Solved: 447[Submit][Status][Di ...

  5. zoj 2369 Two Cylinders

    zoj 2369 Two Cylinders 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2369 题意:已知两个无 ...

  6. CMDB服务器管理系统【s5day88】:采集资产之整合插件

    以后导入配置文件不用去from conf而是导入from lib.config,因为在这可以导入global_settings和settings.py import sys import os imp ...

  7. Java多线程之“同步”

    好习惯要坚持,这是我第二篇博文,任务略重,但是要坚持努力!!! 1.竞争条件 首先,我们回顾一下<Java核心技术卷>里讲到的多线程的“竞争条件”.由于各线程访问数据的次序,可能会产生讹误 ...

  8. table第一行合并,其余行宽度失效问题

    <col>标签 http://www.w3school.com.cn/tags/tag_col.asp 由于表格中有一行合并过,所以其它没有合并的行的列宽就会平均化,对列宽的设置也会失效. ...

  9. python基础---输入输出

    1.输入字符串. name=input()   or name=input('please input a string') 这样可以接收一个字符串,包括空格,都可以输入.只有回车不接受,作为结束符, ...

  10. java enum用法

    基本用法 enum Day { SUNDAY, MONDAY, TUESDAY, WENDSDAY, THURSDAY, FRIDAY, SATURDAY; } 枚举是常量,所以应该用大写. 枚举是对 ...