题目链接: Balanced Substring

题意:

  求一个只有1和0的字符串中1与0个数相同的子串的最大长度。

题解:

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

 #include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5+;
const int INF = 1e5;
char vec[MAX_N];
int res[MAX_N],val[MAX_N*];
int N,M,t,num;
void init()
{
res[] = ;
memset(val,,sizeof(val));
}
int main()
{
cin>>N;
scanf("%s",vec+);
for(int i=;i<=N;i++)
{
if(vec[i] == '') res[i+] = res[i] - ;
else res[i+] = res[i] + ;
val[res[i+] + INF] = i;
}
int ans = -;
for(int i=;i<=N+;i++)
{
ans = max(ans,val[res[i]+INF] - i + );
}
cout<<ans<<endl;
return ;
} /*
18
011010101110111101 ans : 8
*/

Codeforces 873 B. Balanced Substring(前缀和 思维)的更多相关文章

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

  2. CF873B Balanced Substring (前缀和)

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

  3. Educational Codeforces Round 30 B【前缀和+思维/经典原题】

    B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...

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

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

  5. [Codeforces 873B]Balanced Substring

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

  6. 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)

    P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...

  7. 837B. Balanced Substring

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

  8. Balanced Substring

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

  9. 【Cf edu 30 B. Balanced Substring】

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

随机推荐

  1. Jenkins定时构建和轮询SCM设置说明

    看图说事: 一.定时构建:不管SVN或Git中数据有无变化,均执行定时化的构建任务 : 二.轮询SCM:只要SVN或Git中数据有更新,则执行构建任务: 三.构建语法说明: 1.首先格式为:* * * ...

  2. TreeSet集合的add()方法源码解析(01.Integer自然排序)

    >TreeSet集合使用实例 >TreeSet集合的红黑树 存储与取出(图) >TreeSet的add()方法源码     TreeSet集合使用实例 package cn.itca ...

  3. 导出类成员里含有stl对象

    How to export an instantiation of a Standard Template Library (STL) class and a class that contains ...

  4. 【Alpha】Daily Scrum Meeting——blog3

    团队成员 吴吉键 201421122007(组长) 魏修祺 201421122008 孙劲林 201421122022 1. 会议当天照片 2. 每个人的工作 姓名 已完成工作 计划完成工作 遇到的困 ...

  5. Spring MVC Interceptor

    1 在spring-servlet.xml中进行如下配置 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path=& ...

  6. Java虚拟机17:互斥同步、锁优化及synchronized和volatile

    互斥同步 互斥同步(Mutual Exclusion & Synchronization)是常见的一种并发正确性保证手段.同步是指子啊多个线程并发访问共享数据时,保证共享数据在同一时刻只能被一 ...

  7. (动态)代理于HOOK的区别于关系

    代理模式是MITM中间人攻击模式: 是拦截对象的所有交互,然后进行处理转发: HOOK模式是定点拦截,只针对单个函数做处理转发: HOOK模式可以在动态代理模式基础上实现:因为代理模式拦截所有.

  8. apache ActiveMQ之初体验

    版权声明: https://blog.csdn.net/zdp072/article/details/27237549 一. 开篇语 继上一篇weblogic中使用jms发送和接受消息的文章后, 本文 ...

  9. PHP microtime() 函数

    定义和用法 microtime() 函数返回当前 Unix 时间戳和微秒数. 语法 microtime(get_as_float) 参数 描述 get_as_float 如果给出了 get_as_fl ...

  10. weblogic之CVE-2018-3246 XXE分析

    通过ftp通道将数据传出来.上传1.xml <!DOCTYPE xmlrootname [<!ENTITY % aaa SYSTEM "http://192.168.172.12 ...