Educational Codeforces Round 30 B【前缀和+思维/经典原题】
1 second
256 megabytes
standard input
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 equals tor - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.
You have to determine the length of the longest balanced substring of s.
The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s.
The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.
If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.
8
11010111
4
3
111
0
In the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.
In the second example it's impossible to find a non-empty balanced substring.
【题意】:一个01字符串,求出现0、1出现次数相等的最长子串的长度。
【分析】:
change array a[]==0 to -1.find prefix[]=prefix sum
find two indices of maximum difference for which it is same value.
take example.
1 0 0 1
1 -1 -1 1
prefix[]=1 0 -1 0
思路:如果某子串中0与1的个数相等那么该子串的代数和肯定等于0。如果最长子串是从开头开始的那么当代数和等于0出现的最后边的位置即最长子串的末尾,该子串即最长子串;如果不是从开头开始的,那么最长子串即代数和相等的两个位置的距离最长的那个子串。用一个数组mp[]来记录加和首次出现的位置,如果该加和还没有出现过就记录下来此时位置,由于是从前往后遍历的,则首次记录的位置肯定是最前边的位置,那么同等加和出现的位置越往后则两者距离就越长。详细请看代码即代码旁的注释。
【代码】:
#include <bits/stdc++.h> using namespace std; map<int,int> mp;
int a[+];//前缀和数组
int main()
{
int n;
char c;
cin>>n;
int sum=;
for(int i=;i<=n;i++)
{
scanf(" %c",&c);
if(c=='')
sum++;
else
sum--;
a[i]=sum;//将前缀和装入a数组
if(!mp[sum])//如果此时的代数和之前没出现过则记录下来此时的位置
mp[sum]=i;
}
mp[]=;//前缀和位置数组
int ans=;
for(int i=;i<=n;i++)
ans=max(ans,i-mp[a[i]]);//如果此代数和之前出现过就计算一下当前位置距离首次此代数和出现位置有多远,然后再与ans比较取最大
cout<<ans;
return ;
}
遇见一个1,sum++,遇见0,sum--;记录每一个的sum的位置,,dis[sum]=i; 初始的时候把dis全部初始化为-1,如果当前dis[sum]= -1证明这个sum第一次出现,记录其出现的位置。如果当前did[sum]!=-1,那么证明这个sum,之前出现过,那么从之前那个位置到现在这个位置,中间的和为0,sum+0=sum 证明中间是一部分平衡01串,然后一直更新,最后的结果就是最长的平衡01串。类似组合数学中做鸽巢定理一道题的思路。因为有负数的存在,所以每个sum+1e5。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
char num[]; //存储01数字串
int dis[]; //记录代数和首次出现的位置
int main()
{
//freopen("lalala.text","r",stdin);
while(~scanf("%s",num))
{
memset(dis,-,sizeof(dis)); //初始化为-1
int len=strlen(num);
int sum=,mm=; //sum记录从开头到当前位置的代数和,mm代表此时最长子串的长度
for(int i=; i<len; i++)
{
if(num[i]=='') //如果是1就+1
sum++;
else if(num[i]=='') //如果是0就-1
sum--;
if(sum==) //如果代数和是0就说明该最长子串是从头开始的
{
mm=max(mm,i+); //只要出现0就比一下最长长度
continue;
}
if(dis[sum+]==-) //为了以防出现负数sum+1000000保证下标始终为正;如果此时的代数和之前没出现过则记录下来此时的位置
dis[sum+]=i;
else //如果此代数和之前出现过就计算一下当前位置距离首次此代数和出现位置有多远,然后再与mm比较取最大
{
mm=max(mm,i-dis[sum+]);
}
}
printf("%d\n",mm);
}
return ;
}
http://blog.csdn.net/qq_34131212/article/details/78229621
Educational Codeforces Round 30 B【前缀和+思维/经典原题】的更多相关文章
- Educational Codeforces Round 30
Educational Codeforces Round 30 A. Chores 把最大的换掉 view code #pragma GCC optimize("O3") #pr ...
- Educational Codeforces Round 40 C. Matrix Walk( 思维)
Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...
- Educational Codeforces Round 30 D. Merge Sort
题意:给你n和k,n代表有多少个数,k代表几次操作,求一个1到n的序列,要k次mergesort操作才能还原 Examples Input 3 3 Output 2 1 3 Input 4 1 Out ...
- Educational Codeforces Round 30 A[水题/数组排序]
A. Chores time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Educational Codeforces Round 57D(DP,思维)
#include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...
- Educational Codeforces Round 9 A. Grandma Laura and Apples 水题
A. Grandma Laura and Apples 题目连接: http://www.codeforces.com/contest/632/problem/A Description Grandm ...
- Educational Codeforces Round 22 E. Army Creation(分块好题)
E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 59 (Rated for Div. 2) (前四题)
A. Digits Sequence Dividing(英文速读) 练习英语速读的题,我还上来昏迷一次....只要长度大于2那么一定可以等于2那么前面大于后面就行其他no 大于2的时候分成前面1个剩下 ...
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
随机推荐
- [洛谷P3153] [CQOI2009]跳舞
题目大意:有n个女生,n个男生,每次一男一女跳舞.同一队只会跳一次.每个男孩最多只愿意和k个不喜欢的女孩跳舞,女孩同理.问舞会最多能有几首舞曲? 题解:二分跳了多少次舞,每次重建图,建超级原点和汇点, ...
- display:inline-block带来的问题及解决办法
在日常工作中,会经常遇到两个或多个元素并排排列的效果,以前会使用float等实现,float虽然方便好用,但是需要清除浮动,有时会带来意想不到的bug 而且在移动端是不推荐使用float的,所以使用d ...
- (一)STM32固件库详解(转载)
本篇博文是转载自emouse,因为不能直接转载,所以是复制过来再发布的. emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 1.1 基于标准外设库的 ...
- 怎么把linux的磁盘映射到windows上
步骤如下: 右击如下的computer: 然后选择:Map network drive... 然后在下图按图中所示操作: 最后成功如下图所示:
- 利用WebStorm来管理你的Github
什么是Github Github是一个共享虚拟主机服务,用于存放使用Git版本控制的软件代码和内容项目,以最简单的方式来说,其实就是一个代码库,上面有全世界无数优秀的码农上传自己的作品和大家共享(当然 ...
- 设置查看java的源程序
1.点 “window”-> "Preferences" -> "Java" -> "Installed JRES" 2. ...
- HDFS之FileSystem
package cn.hx.test; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; impo ...
- bzoj 2668 费用流
我们可以把初始状态转化为目标状态这一约束转化为将黑子移动到目标状态所需要的最少步数. 除了初始点和目标点之外,剩下的点如果被经过那么就会被交换两次,所以我们将一个点拆成3个点,a,b,c,新建附加源点 ...
- bzoj 1293 贪心
首先我们可以将这道题看成一个数轴,数轴其中的某些点存在一些颜色,我们要选取最短的一段,使这段存 在所有颜色,那么我们使用指针i,j表示在j-i位置中包含的颜色,那么初值是0,0,我们先i++,同时添加 ...
- 遍历文档内容,得到HTML层级结构
嗯..没发现有写好的,那就自己写一个,刚好自己今天看了DOM操作的知识点,巩固一下. HTML可以表示为一个层次结构,生成的DOM Tree 就是类似与数据结构中的树一样,每个DOM节点都有它的chi ...