837B. Balanced Substring
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 to r - 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
inputCopy
8
11010111
outputCopy
4
inputCopy
3
111
outputCopy
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.
题目就是要你找出最长的0的数量和1的数量相等的长度是多少
唉,一开始觉得2分能做,还做了半天,后面发现有问题有找不出答案,后面一看结果太简单了
把0当成-1,建一个结构体,存前缀和,并保持当前的位置的序号,然后sort,前缀和相同的位置序号相减,保留最大值就好了,第0个为0,0,否则有问题
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<stdio.h>
#include<float.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define sf scanf
#define pf printf
#define fi first
#define se second
#define mp make_pair
#define pii pair<int,int>
#define scf(x) scanf("%d",&x)
#define prf(x) printf("%d\n",x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define mm(x,b) memset((x),(b),sizeof(x))
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
typedef long long ll;
const ll mod=1e9+7;
using namespace std;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int N=2e5+10;
char a[N];
struct node{
int num;
int pos;
}q[N];
bool cmp(node x,node y)
{
if(x.num == y.num ) return x.pos < y.pos ;
return x.num <y.num ;
}
int main()
{
int n;scf(n);
sf("%s",a+1);
rep(i,0,n+1)
{
if(i==0)
{
q[i].num = 0;
q[i].pos= 0;
}else
{
if(a[i]=='1')
{
q[i].num = q[i-1].num +1;
}else
{
q[i].num = q[i-1].num -1;
}
q[i].pos=i;
}
}
sort(q,q+n+1,cmp);
int ans=0;
rep(i,1,n+1)
{
if(q[i].num == q[i-1].num )
{
int j=i;
while(q[j].num ==q[i-1].num&&j<=n) j++;
j--;
ans=max(ans,q[j].pos -q[i-1].pos);
i=j;
}
}
prf(ans);
return 0;
}
837B. Balanced Substring的更多相关文章
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- Balanced Substring
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...
- 【Cf edu 30 B. Balanced Substring】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CF873B Balanced Substring (前缀和)
CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...
- codefroces 873 B. Balanced Substring && X73(前缀和思想)
B. Balanced Substring You are given a string s consisting only of characters 0 and 1. A substring [l ...
- CF873B Balanced Substring
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...
- Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
随机推荐
- L2-011 玩转二叉树 (25 分) (树)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784 题目: 给定一棵二叉树的中序遍历和前序 ...
- typescript解决深度拷贝中循环引用引起的死循环
循环引用有人说就是一种不健康的状态,即你中有我,我中有你 hasObj: any = []; deepCopy(data: any) { this.hasObj.push(data); //最终就是返 ...
- Linux下执行自定义的可执行命令无效原因
1 前言 用golang编译成可执行文件tt stats text.txt(tt 是编译后重命名的可执行文件),然后直接执行失败了,后来使用./tt stats text.txt可以了. 执行结果如下 ...
- python获取设备主机名和IP地址
import socket def print_machine_info(): host_name = socket.gethostname() ip_address = socket.gethost ...
- el-table中单数行与双数行设置不同的背景颜色
<el-table :cell-style='cellStyle' :data="tableData" style="width: 100%;" > ...
- VS Code 1.28版本设置中文界面的方法
最近将vscode升级到1.28版本,发现升级后默认界面变成英文了,而且在按照网上的说法在locale.json设置locale: "zh-cn"也不起效,解决的解决方法很简单: ...
- 【转载】OpenSSL 提取 pfx 数字证书公钥与私钥
转自https://www.cnblogs.com/Irving/p/9551110.html OpenSSL 提取 pfx 数字证书公钥与私钥 由于之前生产环境已经使用了 Identityser ...
- nginx 平滑更新
1.更新nginx源 #centos6的nginx源 #centos7的话吧url 的6改为7就行了 vim /etc/yum.repos.d/nginx.repo [nginx] name=ngin ...
- MyBatis异常:元素内容必须由格式正确的字符数据或标记组成
今天在写接口查询SQL时,报了一个异常,如下: Cause: org.apache.ibatis.builder.BuilderException: Error creating document i ...
- Android Studio编译卡死
首先,用AS,你必须fanqiang,其它都是次要的. AS/bin/*.exe.vmoptions ## *DO NOT* modify this file directly. If there i ...