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

题干:

链接: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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439905.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2.1)深度学习笔记:深度学习的实践层面

目录 1&#xff09;Train/Dev/Test sets 2&#xff09;Bias/Variance 3&#xff09;Regularization&#xff08;重点&#xff09; 4&#xff09;Why regularization reduces overfitting&#xff08;理解&#xff09; 5&#xff09;Dropout Regularization&#xff08;重点…

一步步编写操作系统 12 代码段、数据段、栈和cpu寄存器的关系

先说下什么是寄存器。 寄存器是一种物理存储元件&#xff0c;只不过它是比一般的存储介质要快&#xff0c;能够跟上cpu的步伐&#xff0c;所以在cpu内部有好多这样的寄存器用来给cpu存取数据。 先简短说这一两句&#xff0c;暂时离开一下主题&#xff0c;咱们先看看相对熟悉一…

【2019牛客暑期多校训练营(第三场)- F】Planting Trees(单调队列,尺取)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/883/F 来源&#xff1a;牛客网 The semester is finally over and the summer holiday is coming. However, as part of your universitys graduation requirement, you have to take part in some …

Apollo进阶课程⑯丨Apollo感知之旅——感知概貌

原文链接&#xff1a;进阶课程⑯丨Apollo感知之旅——感知概貌 上周阿波君为大家详细介绍了「进阶课程⑮| Apollo无人车自定位技术入门」。 我们人类天生就配备多种传感器&#xff0c;眼睛可以看到周围的环境&#xff0c;耳朵可以用来听&#xff0c;鼻子可以用来嗅&#xff0c;…

一步步编写操作系统 13 栈

栈到底是什么玩意 cpu中有栈段SS寄存器和栈指针SP寄存器&#xff0c;它们是用来指定当前使用的栈的物理地址。换句话说&#xff0c;要想让cpu运行&#xff0c;必须得有栈。栈是什么?干吗用的&#xff1f;本节将给大家一个交待。 还记得数据结构中的栈吗&#xff1f;那是逻辑…

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

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/882/E?&headNavacm 来源&#xff1a;牛客网 Given a maze with N rows and M columns, where bijb_{ij}bij​ represents the cell on the i-row, j-th column. If bi,j"1"b_{i, j} …

Apollo进阶课程⑰丨Apollo感知之旅——传感器选择和安装

目录 1.激光雷达 2.相机 3.Radar毫米波 4.安装传感器 原文链接&#xff1a;进阶课程⑰丨Apollo感知之旅——传感器选择和安装 上周阿波君为大家详细介绍了「进阶课程⑯ Apollo感知之旅——感知概况」。 传感器是一种检测装置&#xff0c;能感受到被测量的信息&#xff0c;…

一步步编写操作系统 14 CPU与外设通信——IO接口 上

介绍显卡之前&#xff0c;必须得和大家交待清楚&#xff0c;那么多的外部设备&#xff0c;cpu是如何与他们交流。 大家都学过微机接口技术吧&#xff1f;没学过也没关系&#xff0c;反正我也只是笼统地说说^_^&#xff0c;保证大家一定能看得懂。 按理说&#xff0c;如果硬件…

2.2)深度学习笔记:优化算法

目录 1&#xff09;Mini-batch gradient descent&#xff08;重点&#xff09; 2&#xff09;Understanding mini-batch gradient descent 3&#xff09;Exponentially weighted averages 4&#xff09;Understanding exponetially weighted averages 5&#xff09;Bias c…

【POJ - 2019】Cornfields(二维st表,模板)

题干&#xff1a; FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, hes looking to build the cornfield on the flattest piece of land he can find. FJ has, at great expense, surveyed his square fa…

虚拟机安装Linux(vmware + ubuntu)

VMWare 提取码&#xff1a;7zph 建议官网下载比较新&#xff0c;还快一点 https://www.vmware.com/products/workstation-pro.htmlubantu 下载地址 安装过程都差不多可以参考 VMware下安装Ubuntu系统图文详细教程_master-CSDN博客_vmware安装ubuntu系统 出现蓝屏问题可以参考…

Apollo进阶课程⑱丨Apollo感知之旅——传感器标定

目录 传感器标定 标定的目的 传感器标定算法 标定案例解析 3D标定间制作 Cmaera-to-Camera外参标定 Lidar-to-Camera外参标定 Lidar-to-Lidar外参标定 Lidar内参标定 Lidar-to-GPS外参标定 自然场景的Lidar-to-Camera外参标定 自然场景的Bifocal Camera外参标定 C…

一步步编写操作系统 15 CPU与外设通信——IO接口,下

既然都说到IO接口了&#xff0c;不知道各位有没有疑问&#xff0c;cpu是怎样访问到IO接口呢&#xff1f;肯定得有个链路吧&#xff1f;什么&#xff1f;有隐约听到有同学开玩笑说&#xff1a;cpu用无线访问其它设备。哈哈&#xff0c;不知道各位听说过没有&#xff0c;无线的终…

Telnet端口连接Linux服务器失败

在ubuntu写了个服务器端口号是666 &#xff0c;ip地址是192.168.96.129 在windows用telnet无法连接上 首先检查windows telnet服务是否打开 Windows 10操作系统上使用telnet命令&#xff08;图文&#xff09;_时间-CSDN博客_windows使用telnet命令 测试网络是否通&#xff1a;…

*【2019牛客暑期多校训练营(第三场)- G】Removing Stones(分治)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/883/G 来源&#xff1a;牛客网 Summer vacation is coming and Mark has returned home from his university having successfully survived the exam week. Today, he is very bored. So his frien…

重磅 | 完备的 AI 学习路线,最详细的资源整理!

本文转自微信公众号&#xff1a;Datawhale&#xff08;强烈推荐&#xff09; 原创&#xff1a; AIUnion Datawhale 今天 【导读】 本文由知名开源平台&#xff0c;AI技术平台以及领域专家&#xff1a;Datawhale&#xff0c;ApacheCN&#xff0c;AI有道和黄海广博士联合整理贡献…

一步步编写操作系统 16 显卡概述

之前我们的mbr中我们刚刚向屏幕输出了“1 MBR”这几个字符&#xff0c;这种喜悦还没有过去&#xff0c;我就要给大家泼冷水了&#xff1a;这种打印字符的方法马上就用不了啦。 mbr是运行在实模式下&#xff0c;所以在实模式下也可以用bios的0x10中断打印字符串&#xff0c;这是…

Windows/Linux 下使用telnet发送消息

Windows下使用telnet 1.首先打开cmd命令行连接上服务器端口 连不上可以参考这篇 Telnet端口连接Linux服务器失败_m0_46480482的博客-CSDN博客 telnnt <ip地址> <端口号> 2. 连接成功后&#xff0c;会发现是一片黑的 按住 ctrl ] 可以招出提示 输入 &#x…

【2019牛客暑期多校训练营(第六场)- D】Move(随机化二分)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/886/D 来源&#xff1a;牛客网 After the struggle of graduating from college, TangTang is about to move from a student apartment to his new home. TangTang has n items to move, the i-th …

Apollo进阶课程⑲丨Apollo感知之旅——感知算法

目录 点云感知 启发式方法&#xff1a;NCut 深度学习方法&#xff1a;CNNSeg 视觉感知 CNN检测 CNN分割 后处理 红绿灯感知 基于深度学习的红绿灯感知模块 Radar感知 超声波感知 原文链接&#xff1a;进阶课程⑲丨Apollo感知之旅——感知算法 感知是自动驾驶的第一环…