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)