题目链接:
http://codeforces.com/problemset/problem/873/B
题解:
前缀和+离散化,dp的暴力更新。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define met(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int maxn = 1e5+10;
char s[maxn];
int dp[maxn*2];
int main()
{
int n;
scanf("%d",&n);
scanf("%s",s);
int ans=0;
int MAX=0;
met(dp,-1);
for(int i=0;i<n;i++)
{
if(s[i]=='1')
ans+=1;
if(s[i]=='0')
ans+=(-1);
if(ans==0)
MAX=max(MAX,i+1);
if(dp[ans+100000]==-1)
dp[ans+100000]=i;
else
MAX=max(MAX,i-dp[ans+100000]);
}
printf("%d\n",MAX);
}