cf873b Balanced Substring (prefix and)

Source: Internet
Author: User

cf873b Balanced Substring (prefix and)

It's a very interesting question, but it's still ... a lot of fun to me. Because the CF evaluation is bad, have not tried whether can be too.
Obviously beg \ (\sum[i][0]-\sum[l][0] = \sum[i][1]-\sum[l][1]\)
\ (\sum[i][0]-\sum[l][1] = \sum[i][0]-\sum[l][0]\)
Then hash the DP.

#include <iostream> #include <cstdio>using namespace std; #define REP (i,x,p) for (int i = x;i <= p;++ i) const int MAXN = 100000 + 7;/* State Sum[i]sum[i][0] Indicates the number of front I bits 0 number sum[i][1] Indicates the number of previous I bits 1 number */int f[maxn][2];                F[i][0] represents the farthest position of sum[i][0]-sum[i][1]. F[I][1] represents the farthest position of sum[i][1]-sum[i][0]. int a[maxn];char c[maxn];int sum[maxn][2];inline int max (int a,int b) {return a ; B? A:B;} inline int min (int a,int b) {return a > b b:a;}    inline int read () {int x = 0,f = 1;char c = GetChar ();    while (C < ' 0 ' | | c > ' 9 ') {if (c = = '-') F = -1;c = GetChar ();}    while (c >= ' 0 ' && C <= ' 9 ') {x = x * + C-' 0 '; c = GetChar ();} return x * f;}    int main () {int n = 0,ans = 0;    n = read ();    Rep (i,1,n) scanf ("%1d", &a[i]);        Rep (i,1,n) {sum[i][0] = sum[i-1][0];        SUM[I][1] = sum[i-1][1]; A[i] = = 0?        Sum[i][0] + +: sum[i][1] + +;            if (Sum[i][0] > sum[i][1]) {int tmp = sum[i][0]-sum[i][1]; If(F[tmp][0]) ans = max (ans,i-f[tmp][0]);        else f[tmp][0] = i;            } else {int tmp = sum[i][1]-sum[i][0];            if (f[tmp][1]) ans = max (ans,i-f[tmp][1]);        else f[tmp][1] = i;    }} printf ("%d\n", ans); return 0;}

cf873b Balanced Substring (prefix and)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.