CF873B Balanced Substring】的更多相关文章

CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] - \sum[l][0] = \sum[i][1] - \sum[l][1]\) \(\sum[i][0] - \sum[l][1] = \sum[i][0] - \sum[l][0]\) 然后hash一下DP即可. #include <iostream> #include <cstdio…
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\((j-1)\)替换为\(j\) \[2*sum[i]-2*sum[j]=i-j\] \[2*sum[i]-i=2*sum[j]-j\] 枚举i,然后求前面和\(2*sum[i]-i\)相同的最小标号 这里可能有负数,所以map就好了 当然,因为\(2*sum[i]-i\)的值是在区间[-n,n]的 所以你可…
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number…
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals t…
inputstandard input outputstandard output You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2… sr, and its length equals to r - l + 1. A substring is called balanced if the number of zero…
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置.因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度.但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0.我就是在这个地方错了,这里给出错地方的数据. #include<bits…
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in t…
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equa…
B. Balanced Substring You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to t…
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则称为平衡子串,求最长的平衡子串. 解题思路:将0换成-1,算出每个点的前缀和,若两个点前缀和相同,从第一个点到第二个点之前的字符串则为平衡串,求出最长的即可. 代码: #include<iostream> #include<cstring> #include<cstdio>…