题干:

链接:https://ac.nowcoder.com/acm/contest/883/B
来源:牛客网

ZYB loves binary strings (strings that only contains `0' and `1'). And he loves equal binary strings\textit{equal binary strings}equal binary strings more, where the number of `0' and the number of `1' in the string are equal.

ZYB wants to choose a substring from an original string  T\ T T so that it is an equal binary string\textit{equal binary string}equal binary string with the longest length possible. He also wants to choose a subsequence of  T\ T T which meets the same requirements.

A string  v\ v v is a substring of a string  w\ w w if  v\ v v is empty, or there are two integers  l\ l l and r (1≤l≤r≤∣w∣)r \ (1 \le l \le r \le |w|)r (1≤l≤r≤∣w∣) such that v=wlwl+1⋯wrv=w_lw_{l+1}\cdots w_rv=wl​wl+1​⋯wr​. A string  v\ v v is a subsequence of a string  w\ w w  if it can be derived from  w\ w w  by deleting any number (including zero) of characters without changing the order of the remaining characters.

For simplicity, you only need to output the maximum possible length. Note that the empty string is both a substring and a subsequence of any string.

输入描述:

The first line of the input contains a single integer N (1≤N≤100000)N \ (1 \le N \leq 100000)N (1≤N≤100000), the length of the original string  T\ T T. The second line contains a binary string with exactly  N\ N N characters, the original string  T\ T T.

输出描述:

Print two integers  A\ A A and  B\ B B, denoting the answer for substring and subsequence respectively.

示例1

输入

复制

8
01001001

输出

复制

4 6

题目大意:

给你一个01字符串,让你求01个数相等的子串和子序列长度。

解题报告:

对于子序列肯定好说,直接就是0和1取个最大值再乘以2就是答案。

对于子串,正常想法是对于偶数长度进行二分,但是其实是没有单调性的,比如这个样例:00111100,长度为6是不成立的,长度为8是成立的。所以正解是用前缀和,记录第一个出现的位置即可。注意初始化0的初始位置是0。因为他可以从头开始取。注意取长度的时候不需要再+1了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
const int ZERO = 1e5;
int sum[MAX];
int pos[MAX];
int num[2],n;
char s[MAX];
int main()
{memset(pos,-1,sizeof pos);cin>>n;scanf("%s",s+1);for(int i = 1; i<=n; i++) s[i] -= '0',num[s[i]]++,sum[i] = sum[i-1] + (s[i] == 0 ? -1 : 1);int ans2 = min(num[0],num[1])*2;int ans1 = 0;pos[ZERO] = 0;for(int i = 1; i<=n; i++) {if(pos[ZERO+sum[i]] != -1) ans1 = max(ans1,i - pos[ZERO + sum[i]]);else pos[ZERO+sum[i]] = i;}printf("%d %d\n",ans1,ans2);return 0 ;
}

【2019牛客暑期多校训练营(第三场)- B】Crazy Binary String(思维,01串,前缀和)相关推荐

  1. 2019牛客暑期多校训练营 第三场 I Median

    传送门 链接:https://ac.nowcoder.com/acm/contest/883/I 来源:牛客网 JSB has an integer sequence a1,a2,-,ana_1, a ...

  2. 【2019牛客暑期多校训练营(第二场) - H】Second Large Rectangle(单调栈,全1子矩阵变形)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/H 来源:牛客网 题目描述 Given a N×MN \times MN×M binary matrix. ...

  3. 2019牛客暑期多校训练营(第一场)E-ABBA(dp)

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  4. 2019牛客暑期多校训练营(第一场)

    传送门 参考资料: [1]:官方题解(提取码:t050 ) [2]:标程(提取码:rvxr ) [3]:牛客题解汇总 A.Equivalent Prefixes(单调栈) •题意 定义两个数组 u,v ...

  5. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  6. 【2019牛客暑期多校训练营(第二场)- E】MAZE(线段树优化dp,dp转矩阵乘法,线段树维护矩阵乘法)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/E?&headNav=acm 来源:牛客网 Given a maze with N rows an ...

  7. 【2019牛客暑期多校训练营(第二场)- F】Partition problem(dfs,均摊时间优化)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/F 来源:牛客网 Given 2N people, you need to assign each of ...

  8. 【2019牛客暑期多校训练营(第二场) - D】Kth Minimum Clique(bfs,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/D 来源:牛客网 Given a vertex-weighted graph with N vertice ...

  9. 【2019牛客暑期多校训练营(第一场) - A】Equivalent Prefixes(单调栈,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Two arrays u and v each with m distinct elem ...

  10. 【2019牛客暑期多校训练营(第一场) - H】XOR(线性基,期望的线性性)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/H 来源:牛客网 Bobo has a set A of n integers a1,a2,-,ana1, ...

最新文章

  1. JavaScript面向对象编程——Array类型
  2. 信息系统运维安全管理规定(可作为范文参考)
  3. C++ Primer 5th笔记(chap 10)泛型算法 :算法形参
  4. ES6的新特性(3)——变量的解构赋值
  5. android js 弹窗,Android WebView 不能弹出alert的对话框
  6. USACO 1.1 Friday the Thirteenth
  7. Wait Event SQL*Net more data to client
  8. LeetCode 287. 寻找重复数(BitMap)
  9. 贪吃蛇程序 php,微信小程序-贪吃蛇教程实例
  10. jsp 定义java类_在JSP中定义一个类
  11. Java中Spring报错org.springframework.core.annotation.AnnotationUtils.clearCache()V
  12. 32个设计非常精美的国外网站作品范例(下篇)
  13. 手把手教你如何破解无线路由密码
  14. 大话设计模式之爱你一万年:第十三章 行为型模式:策略模式:女友在手,说走就走:1.策略模式概念
  15. Proofs for Inner Pairing Products and Applications 学习笔记
  16. 【Markdown语法】字体颜色大小及文字底色设置
  17. 解决win10系统桌面应用图标显示不出来的问题
  18. Yii2 User 登录原理
  19. 联想Y7000安装Ubuntu16.04/Win10双系统,wifi问题,显卡驱动和CUDA10安装
  20. 机器学习深度学习视频资料汇总

热门文章

  1. 修改 MrBayes 3.2 源码解决不能恢复断点的问题
  2. python wait_Python的等效Java函数wait(),notify(),synchronized
  3. android5.1 显示方向,Android5.1 Settings.apk定制显示选项
  4. .net千万级数据导出_记一次解决docker下oracle数据库故障事例
  5. c语言凸包算法,基于C语言的凸包算法实现
  6. Tecplot如何提取某点数据并导出
  7. 活体检测python_活体检测很复杂?仅使用opencv就能实现!(附源码)!
  8. linux设置时间为24小时制,设置时区
  9. ZDB5304烧写方法
  10. WinCE中得Catalog Items前的标记图标的意义总结