题意:
如题。
或者用我的数组分治也可以,就是有点愚蠢。
1 //#include <bits/stdc++.h> 2 #include <map> 3 #include <iostream> 4 #include <algorithm> 5 /* 6 在O(n)中找连续的子串 -> 统计前缀和 map记录下标 7 */ 8 using namespace std; 9 char s[100005]; 10 int n; 11 int zero = 0, one = 0; 12 int sum[100005]; 13 map<int, int> mp1, mp2; 14 int main() 15 { 16 cin >> n; 17 cin >> (s + 1); 18 19 20 sum[0] = 0; 21 mp1[0] = 0; 22 for (int i = 1; i <= n; i++) 23 { 24 if (s[i] == '1') 25 { 26 sum[i] = sum[i - 1] +1; 27 one++; 28 } 29 else 30 { 31 sum[i] = sum[i - 1] -1; 32 zero++; 33 } 34 35 if (mp1.count(sum[i])) 36 mp2[sum[i]] = i; 37 else mp1[sum[i]] = i; 38 } 39 int ans = 0; 40 41 for (auto i : mp2) 42 { 43 int t = i.second - mp1[i.first]; 44 ans = max(ans, t); 45 //cout << i.second << ' ' << mp1[i.first] << endl; 46 } 47 cout << ans << ' ' << min(zero, one) * 2; 48 49 50 51 }