牛客多校__Crazy Binary String

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

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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

弄个前缀和,然后出现相同前缀的,把坐标相减去最大

#include <bits/stdc++.h>

using namespace std;

const int maxn = 3e5+9;

char s[maxn];
int a[maxn];

int main(){
    int n;
    cin>>n;
    int len=0;
    scanf("%s",s+1);
    memset(a,-1,sizeof(a));
    int sum=100000;
    a[100000]=0;
    int cnt=0;
    for(int i=1;s[i];i++){
        if(s[i]=='0'){
            cnt++;
            sum--;
            if(a[sum]==-1)a[sum]=i;
        }
        else{
            sum++;
            if(a[sum]==-1)a[sum]=i;
        }
        len=max(len,i-a[sum]);
    }
    printf("%d %d\n",len,min(cnt,n-cnt)*2);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值