B. Balanced Substring
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 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.

Input

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.

Output

If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.

Examples
input
  1. 8
    11010111
output
  1. 4
input
  1. 3
    111
output
  1. 0
Note

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[]来记录加和首次出现的位置,如果该加和还没有出现过就记录下来此时位置,由于是从前往后遍历的,则首次记录的位置肯定是最前边的位置,那么同等加和出现的位置越往后则两者距离就越长。详细请看代码即代码旁的注释。

【代码】:

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. map<int,int> mp;
  6. int a[+];//前缀和数组
  7. int main()
  8. {
  9. int n;
  10. char c;
  11. cin>>n;
  12. int sum=;
  13. for(int i=;i<=n;i++)
  14. {
  15. scanf(" %c",&c);
  16. if(c=='')
  17. sum++;
  18. else
  19. sum--;
  20. a[i]=sum;//将前缀和装入a数组
  21. if(!mp[sum])//如果此时的代数和之前没出现过则记录下来此时的位置
  22. mp[sum]=i;
  23. }
  24. mp[]=;//前缀和位置数组
  25. int ans=;
  26. for(int i=;i<=n;i++)
  27. ans=max(ans,i-mp[a[i]]);//如果此代数和之前出现过就计算一下当前位置距离首次此代数和出现位置有多远,然后再与ans比较取最大
  28. cout<<ans;
  29. return ;
  30. }

遇见一个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。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define ll long long
  5. using namespace std;
  6. char num[]; //存储01数字串
  7. int dis[]; //记录代数和首次出现的位置
  8. int main()
  9. {
  10. //freopen("lalala.text","r",stdin);
  11. while(~scanf("%s",num))
  12. {
  13. memset(dis,-,sizeof(dis)); //初始化为-1
  14. int len=strlen(num);
  15. int sum=,mm=; //sum记录从开头到当前位置的代数和,mm代表此时最长子串的长度
  16. for(int i=; i<len; i++)
  17. {
  18. if(num[i]=='') //如果是1就+1
  19. sum++;
  20. else if(num[i]=='') //如果是0就-1
  21. sum--;
  22. if(sum==) //如果代数和是0就说明该最长子串是从头开始的
  23. {
  24. mm=max(mm,i+); //只要出现0就比一下最长长度
  25. continue;
  26. }
  27. if(dis[sum+]==-) //为了以防出现负数sum+1000000保证下标始终为正;如果此时的代数和之前没出现过则记录下来此时的位置
  28. dis[sum+]=i;
  29. else //如果此代数和之前出现过就计算一下当前位置距离首次此代数和出现位置有多远,然后再与mm比较取最大
  30. {
  31. mm=max(mm,i-dis[sum+]);
  32. }
  33. }
  34. printf("%d\n",mm);
  35. }
  36. return ;
  37. }

http://blog.csdn.net/qq_34131212/article/details/78229621

Educational Codeforces Round 30 B【前缀和+思维/经典原题】的更多相关文章

  1. Educational Codeforces Round 30

    Educational Codeforces Round 30  A. Chores 把最大的换掉 view code #pragma GCC optimize("O3") #pr ...

  2. 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 ...

  3. 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 ...

  4. Educational Codeforces Round 30 A[水题/数组排序]

    A. Chores time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  5. Educational Codeforces Round 57D(DP,思维)

    #include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...

  6. Educational Codeforces Round 9 A. Grandma Laura and Apples 水题

    A. Grandma Laura and Apples 题目连接: http://www.codeforces.com/contest/632/problem/A Description Grandm ...

  7. 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 ...

  8. Educational Codeforces Round 59 (Rated for Div. 2) (前四题)

    A. Digits Sequence Dividing(英文速读) 练习英语速读的题,我还上来昏迷一次....只要长度大于2那么一定可以等于2那么前面大于后面就行其他no 大于2的时候分成前面1个剩下 ...

  9. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

随机推荐

  1. TCP/IP Note4

    TCP/IP邮件 你的电子邮件程序会使用不同的TCP/IP协议: 使用SMTP来发送邮件: 使用POP从邮件服务器下载邮件: 使用IMAP连接到邮件服务器 1. SMTP - 简单邮件传输协议 SMT ...

  2. 周记【距gdoi:110天】

    这两个星期都在复习和考试,进度慢了好多.(考试也觉得似乎不是很理想) 姚老要我们写个程序来应对学校的分班问题.然后我们就脑洞打开准备设计一个.写应用程序应该是很烧时间的吧? 接下来搞搞后缀数组,然后还 ...

  3. vue.js 三种方式安装--npm安装

    Vue.js是一个构建数据驱动的 web 界面的渐进式框架.     Vue.js 的目标是通过简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易上手,便于与第三方库或既有项目整合.     ...

  4. AWS CLI command example

    1.list ec2 instance-id, instance status, type, ip address, name aws ec2 describe-instances --query ' ...

  5. [USACO1.3]虫洞

    Luogu链接 题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他 ...

  6. Web.xml过滤器配置及执行顺序概念

    第一个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain ch ...

  7. NGINX : 如何屏蔽未被定义的虚拟主机的访问

    参考: [ how to prevent undefined server names ] nginx 的默认虚拟主机 Nginx 支持基于域名和端口的虚拟主机(virtual host), 根据获取 ...

  8. 如何使主机和虚拟机IP处于同一网段(内网渗透专用)

    先说一下正常流程: 1.打开虚拟机网络设置选项,选择桥接模式(Bridged)[如果是Kali 2.0的话,执行第一步后就OK了(90%)] 2.打开Kali里面的网络设置 3.设置一个ip4或者ip ...

  9. (转)自动安装VIM插件

    转自: http://xwz.me/wiki/doku.php?id=vim:plugins 我的插件列表 把下面GetLatestVimScripts.dat放进~/.vim/GetLatest/目 ...

  10. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...