solution.cpp 515 字节
Newer Older
CSDN问答's avatar
CSDN问答 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <string>

using namespace std;

const int N = 1e5 + 10;

int sum[N];  // sum[i] 表示字符串 s 的前 i 个字符的权值和

int main() {
  string s;
  cin >> s;
  int n = s.size();

  // 计算权值和
  for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + (s[i] - '0');

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      // 计算字符串 s[i...j] 的权值和
      int w = sum[j] - sum[i];
      ans = max(ans, w);
    }
  }

  cout << ans << endl;

  return 0;
}