zoukankan      html  css  js  c++  java
  • Codeforces 873B

    题目链接:http://codeforces.com/problemset/problem/873/B

    题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则称为平衡子串,求最长的平衡子串。

    解题思路:将0换成-1,算出每个点的前缀和,若两个点前缀和相同,从第一个点到第二个点之前的字符串则为平衡串,求出最长的即可。

    代码:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 const int N=1e5+5;
     6 int Min[2*N];//Min[i]表示前缀和为i的起始位置
     7 
     8 int main(){
     9     memset(Min,0x3f,sizeof(Min));
    10     int n,sum=N,ans=0;//sum=N防止下标为负
    11     char ch;
    12     scanf("%d",&n);
    13     getchar();
    14     Min[sum]=0;
    15     for(int i=1;i<=n;i++){
    16         scanf("%c",&ch);
    17         if(ch=='1')
    18              sum++;
    19         else
    20             sum--;
    21         Min[sum]=min(Min[sum],i);
    22         ans=max(ans,i-Min[sum]);
    23     }
    24     printf("%d
    ",ans);
    25     return 0;
    26 }
  • 相关阅读:
    最小生成树计数
    Tree
    NOJ 成绩排名
    NOJ 成绩排名
    NOJ 成绩排名
    NOJ 成绩排名
    NOJ Physics
    NOJ Physics
    NOJ Physics
    NOJ Physics
  • 原文地址:https://www.cnblogs.com/fu3638/p/7689999.html
Copyright © 2011-2022 走看看