题目链接: 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. Oracle重启操作步骤

    有时候在服务中重启了oracle之后,数据库并不能正常访问,可以通过以下步骤: 在windows服务中启动数据库服务: 在windows命令窗口中输入命令:sqlplus /nolog 在sql> ...

  2. (转)光照模型及cg实现

    经典光照模型(illumination model) 物体表面光照颜色由入射光.物体材质,以及材质和光的交互规律共同决定. 由于环境光给予物体各个点的光照强度相同,且没有方向之分,所以在只有环境光的情 ...

  3. win中使用cmd杀端口

    最近在win开发时,总是遇到端口占用的情况...可能是跑的程序太多了吧 当你测试一个demo时遇到这个就很恶心.. 记一下 netstat -ano | findstr 80 //列出进程极其占用的端 ...

  4. FTP工具FileZilla&WinSCP与FTP类库FluentFTP

    FileZilla Filezilla分为client和server.其中FileZilla Server是Windows平台下一个小巧的第三方FTP服务器软件,系统资源也占用非常小,可以让你快速简单 ...

  5. SQL Server之JSON 函数

    SQL Server 2005开始支持XML数据类型,提供原生的XML数据类型.XML索引及各种管理或输出XML格式的函数.随着JSON的流行,SQL Server2016开始支持JSON数据类型,不 ...

  6. 初识android界面布局

    1.活动 活动是android开发中最基本的概念,也是最容易吸引用户的地方,是一种可以包含用户界面的组件. Activity类中定义了7个回调方法,覆盖了活动生命周期的每一个环节.具体如下: (1)o ...

  7. 更改Request Parameters中的值

    1. 定义ParameterRequestWrapper 继承HttpServletRequestWrapper public class ParameterRequestWrapper extend ...

  8. 动态代理与HOOK(与oc isa 替换)

    HOOK:面向函数,解决函数调用拦截与替换的问题: 动态代理:面向对象,解决对象的动态替换问题: 动态代理的实现方案: 1.经典代理机制: 2.子类化机制:oc语言的isa替换是这额解决方案的经典案例 ...

  9. MySQL IFNULL基本用法

    MySQL IFNULL函数是MySQL控制流函数之一,它接受两个参数,如果不是NULL,则返回第一个参数. 否则,IFNULL函数返回第二个参数. 两个参数可以是文字值或表达式. 以下说明了IFNU ...

  10. python3 unittest框架失败重跑加截图支持python2,python3

    github源码地址下载:https://github.com/GoverSky/HTMLTestRunner_cn.git 解压文件后取出/HTMLTestRunner_cn.py文件丢进C:\Py ...