题目链接: 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. 15. DML, DDL, LOGON 触发器

    触发器可以理解为由特定事件触发的存储过程, 和存储过程.函数一样,触发器也支持CLR,目前SQL Server共支持以下几种触发器: 1. DML触发器, 表/视图级有效,可由DML语句 (INSER ...

  2. UNIX高级环境编程(1)File I/O

    引言: Unix系统中主要的文件操作包括: open read write lseek close unbuffered IO和standard I/O相对应,后面的章节我们会讨论这两者的区别. 在讨 ...

  3. 申请Let’s Encrypt永久免费SSL证书过程教程及常见问题

    配置证书https://easy.zhetao.com/   虽然目前Let’s Encrypt免费SSL证书默认是90天有效期,但是我们也可以到期自动续约,不影响我们的尝试和使用,为了考虑到文章的真 ...

  4. EF CodeFirst下的自动迁移

    当我们修改数据模型,添加一个如下字段 再次运行程序,会因为数据库结构与模型不一致而报错 为解决以上错误可以采取以下三种方式 1.  删除数据库,重新运行站点,会重新生成数据库,这样就会丢失数据 2.  ...

  5. November 19th 2016 Week 47th Saturday

    Nature didn't need an operation to be beautiful. It just was. 自然之美无需刻意而为,其本身即为美. Recently I saw seve ...

  6. BZOJ 2763 飞行路线 BFS分层

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2763 题目大意: Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司 ...

  7. iOS网络缓存的系统实现是一个烂尾工程

    烂尾的原因是request的一致性比较接口没有开放出来.

  8. struts2 标签使用注意

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/31954501 通常是用html标签.而不 ...

  9. YII缓存整理

    缓存 缓存是用于提升网站性能的一种即简单又有效的途径.通过存储相对静态的数据至缓存以备所需,我们可以省去生成这些数据的时间.在 Yii 中使用缓存主要包括配置和访问缓存组件 . 如下的应用配置指定了一 ...

  10. centos中安装、升级git

    yum install git 若是从老版本升级,则按下面方法.(centos中) 先更新系统sudo yum update     安装依赖的包yum install curl-devel expa ...