Codeforces 873B - Balanced Substring(思维)
题目链接: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(思维)的更多相关文章
- 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 ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- 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 ...
- CF873B Balanced Substring (前缀和)
CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...
- 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 ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
随机推荐
- 模板:数论 & 数论函数 & 莫比乌斯反演
作为神秘奖励--?也是为了方便背. 所有的除法都是向下取整. 数论函数: \((f*g)(n)=\sum_{d|n}f(d)g(\frac{n}{d})\) \((Id*\mu)(n)=\sum_{d ...
- Navcat中Oracle连接的坑-Instant Client
报错信息: 官方下载Instant Client下载: http://www.oracle.com/technetwork/cn/topics/intel-macsoft-102027-zhs.htm ...
- Android Json解析与总结
一.JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Progra ...
- 纯CSS实现的风车转动效果特效演示
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 分割png图片
/*** * 分割图片 */public function cut_img(){ $filename = 'site_upload/form_file_forinput/20180313/201803 ...
- js处理时间的那些事
我们在实际需求中一般需要对时间进行相应的出来,比如:对时间串的拆分显示,两个时间差的求值显示等. 时间拆分: 一般对于这种处理我们使用正则表示式: 正则表达式拆分时间: var date = data ...
- spoj COT2 - Count on a tree II
COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...
- 【BZOJ1038】【ZJOI2008】瞭望塔 [模拟退火]
瞭望塔 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 致力于建设全国示范和谐小村庄的H村村 ...
- Bzoj4481 [Jsoi2015]非诚勿扰
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 147 Solved: 75 Description [故事背景] JYY赶上了互联网创业的大潮,为非 ...
- 【BZOJ】1096 [ZJOI2007]仓库建设
[算法]DP+斜率优化 [题解]状态转移方程:f[i]=min(f[j]+g(i+1,j-1))+c[i] 关键在于如何O(1)计算g(i+1,j-1). 推导过程:http://blog.csdn. ...