You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include
#include
#include
#include
#include <math.h>
#include
using namespace std;
// 通过计算不同部分连续字符对最终价值的贡献来得到最大的价值。
int findMaxVal(string &s, char c) {
// 1. 找到第一个字符 c 出现的位置 start 和最后一个字符 c 出现的位置 end。
int start = -1, end = -1, num = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == c) {
if (start == -1) start = i;
end = i;
num++;
}
}
// 2. 两个c中间连续字符、前后字符对应的价值
int midVal = (1 + num) * num / 2;
int leftVal = (1 + start) * start / 2;
int len = s.size() - end - 1;
int rightVal = (1 + len) * len / 2;
int totalVal = leftVal + midVal + rightVal;
return totalVal;
}
int main() {
string S;
while (cin >> S) {
int val1 = findMaxVal(S, '0');
int val2 = findMaxVal(S, '1');
cout << max(val1, val2) << endl;
}
}
The text was updated successfully, but these errors were encountered:
#include
#include
#include
#include
#include <math.h>
#include
using namespace std;
// 通过计算不同部分连续字符对最终价值的贡献来得到最大的价值。
int findMaxVal(string &s, char c) {
// 1. 找到第一个字符 c 出现的位置 start 和最后一个字符 c 出现的位置 end。
int start = -1, end = -1, num = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == c) {
if (start == -1) start = i;
end = i;
num++;
}
}
// 2. 两个c中间连续字符、前后字符对应的价值
int midVal = (1 + num) * num / 2;
int leftVal = (1 + start) * start / 2;
int len = s.size() - end - 1;
int rightVal = (1 + len) * len / 2;
int totalVal = leftVal + midVal + rightVal;
return totalVal;
}
int main() {
string S;
while (cin >> S) {
int val1 = findMaxVal(S, '0');
int val2 = findMaxVal(S, '1');
cout << max(val1, val2) << endl;
}
}
The text was updated successfully, but these errors were encountered: