Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: 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(前缀和 思维)的更多相关文章
- 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 ...
- CF873B Balanced Substring (前缀和)
CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...
- Educational Codeforces Round 30 B【前缀和+思维/经典原题】
B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- 洛谷 P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维)
P1360 [USACO07MAR]Gold Balanced Lineup G (前缀和+思维) 前言 题目链接 本题作为一道Stl练习题来说,还是非常不错的,解决的思维比较巧妙 算是一道不错的题 ...
- 837B. Balanced Substring
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Balanced Substring
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...
- 【Cf edu 30 B. Balanced Substring】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- VB ASP 使用 now() 时默认格式调整方法
修改注册表 [HKEY_USERS\.DEFAULT\Control Panel\International] "sShortDate"="yyyy-M-d" ...
- #001 CSS快速入门讲解
CSS入门讲解 HTML人+CSS衣服+JS动作=>DHTML CSS: 层叠样式表 CSS2.0 和 CSS3.0 版本,目前学习CSS2, CSS3只是多了一些样式出来而已 CSS 干啥用的 ...
- RabbitMQ学习以及与Spring的集成(三)
本文介绍RabbitMQ与Spring的简单集成以及消息的发送和接收. 在RabbitMQ的Spring配置文件中,首先需要增加命名空间. xmlns:rabbit="http://www. ...
- Memcahce和Redis比较
一.Memcache 1. memecache 把数据全部存在内存之中,断电后会挂掉,数据不能超过内存大小redis有部份存在硬盘上,这样能保证数据的持久性. 2. Memcache ...
- 【转载】Java 集合框架
http://wangkuiwu.github.io/2012/02/03/collection-03-arraylist/ 网上比较全的Java集合框架教程. 注:transient是Java语言的 ...
- 【问题记录】uwsgi部署并启动俩个几乎一样的python flask web app,发现有一个app响应时间非常长
uwsgi在同一台linux上启动python flask web app(俩个), 发现第一个和第二个的简单性能测试差距非常大,差了将近一倍: 第一个结果: Concurrency Level: 1 ...
- objc_msgSend函数的实现
毕竟汇编语言代码比较晦涩难懂,因此这里将函数的实现反汇编成C语言的伪代码: //下面的结构体中只列出objc_msgSend函数内部访问用到的那些数据结构和成员. /* 其实SEL类型就是一个字符串指 ...
- 2049. [SDOI2008]Cave 洞穴勘测【LCT】
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- TensorFlow函数(十)tf.global_variables_initializer()
tf.global_variables_initializer() 此函数是初始化模型的参数 with tf.Session() as sess: tf.global_variables_initia ...
- virtualbox+vagrant学习-3-Vagrant Share-2-HTTP Sharing
HTTP Sharing Vagrant Share可以创建一个可公开访问的URL端点来访问在Vagrant环境中运行的HTTP服务器.这被称为“HTTP共享”,在使用Vagrant Share时默认 ...