题目大意:
给定长度非0的非空01串S。
求S的非空连续子串T中’0’的个数- '1’的个数的最大值。
解题思路:
O ( n ) O(n) O(n)扫一遍即可
A c c e p t e d c o d e : Accepted\ code: Accepted code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sum, ans;
char s[100005];
int main() {
scanf("%s", s + 1);
int len = strlen(s + 1);
for (int i = 1; i <= len; ++i) {
sum += (s[i] == '0') ? 1 : -1;
ans = max(ans, sum = max(sum, 0));
}
return !printf("%d", ans == 0 ? -1 : ans);
}