思路:一个位num0-num1值=某位num0-num1值相等就代表这段区间内01数字相等,然后还要判断当前位置num0==num1这个情况
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N=1e6+10; char s[N]; int sumone[N]; int sumzero[N]; int mp[2*N]; int main() { int len,x; sumone[0]=0; sumzero[0]=0; scanf("%s",s+1); len=strlen(s+1); int ans=0; memset(mp,-1,sizeof(mp)); for(int i=1;i<=len;i++) { if((s[i]-'0')==0) { sumzero[i]=sumzero[i-1]+1; sumone[i]=sumone[i-1]; x=sumone[i]-sumzero[i]+500000; if(mp[x]==-1) mp[x]=i; else ans=max(ans,i-mp[x]); if(sumone[i]==sumzero[i]) ans=max(ans,i); } else { sumone[i]=sumone[i-1]+1; sumzero[i]=sumzero[i-1]; x=sumone[i]-sumzero[i]+500000; if(mp[x]==-1) mp[x]=i; else ans=max(ans,i-mp[x]); if(sumone[i]==sumzero[i]) ans=max(ans,i); } } printf("%d ",ans); return 0; }