Crazy Binary String

百度(1条未读通知)首页2019牛客暑期多校训练营(第三场)_ACM/noip/noil…B- Crazy Binary String_2019牛客暑期多校训练营(时间限制:CC++1秒,其他语言2秒

空间限制:CC++262144K,其他语言524288K

64bit lO Format: %lld

 

题目描述x

ZYB loves binary strings(strings that only contains 0'and 1). And he loves Itextit(equal binary strings) more, where the number of O' and the number of 1"in the string are equal

ZYB wants to choose a substring from an original string T so that it is an Itextitequal binary string with the longest length possible. He also wants to choose a subsequence of T which meets the

 

same requirements

 

 

A string v is a substring of a string w if v is empty, or there are two integers I andr(1<LSTslwl)such that v =w) wl+I. Wr. A string v is a subsequence of a string w if it can be derived from w

 

输入描述:

 

The first line of the input contains a single integer N(I<N<100000), the length of the original string T. The second line contains a binary

 

string with exactly N characters, the original string T

 

 

 

输出描述

 

 

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

 

 

示例1

 

复制

 

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), the length of the original string T. The second line contains a binary

 

string with exactly N characters, the original string T

 

输入

   8

01001001

输出

 4  6

#include <bits/stdc++.h>
using namespace std;

struct node{
    int maxp,minp;
};
bool cmp(node a,node b){
    return a.maxp-a.minp>b.maxp-b.minp;
}

int main() {
  
    int n,zero_c=0,one_c=0,dif;
    scanf("%d",&n);
    string s;
    cin>>s;
    vector<node>cc(3*n,{-1,-1});
    cc[n+0].minp=0;
    for(int i=0;i<n;i++){
        if(s[i]=='0')zero_c++;
        else if(s[i]=='1') one_c++;
         dif=zero_c-one_c;
        if(cc[n+dif].minp==-1)cc[n+dif].minp=i+1;
        if(cc[n+dif].maxp==-1)cc[n+dif].maxp=i+1;
        if(cc[n+dif].minp>i+1)cc[n+dif].minp=i+1;
        else if(cc[n+dif].maxp<i+1)cc[n+dif].maxp=i+1;
    }
    sort(cc.begin(), cc.end(), cmp);
    printf("%d %d",cc[0].maxp-cc[0].minp,2*min(zero_c,one_c));
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值