题意:
改最多一个位置,使得值最大。值是 连续相同数字个数的平方和
思路:
枚举转折点
#include<cstdio>
#include<algorithm>
#include<string>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
string s;
ll a[N], tot, ans, cnt, cal;
ll get(ll k){
return k * k;
}
int main(){
ios::sync_with_stdio(0);
int T;
cin >> T;
for(int kase = 1; kase <= T; kase++){
cin >> s;
cnt = 1;
memset(a, 0, sizeof(a));
tot = cal = 0;
int len = s.length();
for(int i = 0; i < len; i++){
if(i + 1 == len || s[i] != s[i + 1]){
a[++tot] = cnt;
cal += cnt * cnt;
cnt = 1;
}else cnt++;
}
ans = cal;
for(int i = 1; i < tot; i++) {
if(a[i] == 1) {
ans = max(ans, cal - get(a[i - 1]) - get(a[i + 1]) - 1 + get(a[i - 1] + a[i + 1] + 1));
}else {
if(a[i + 1] != 1 || i == tot - 1) {
ans = max(ans, cal - get(a[i]) - get(a[i + 1]) + get(a[i] + 1) + get(a[i + 1] - 1));
}
ans = max(ans, cal - get(a[i]) - get(a[i + 1]) + get(a[i] - 1) + get(a[i + 1] + 1));
}
}
printf("Case #%d: %lld\n", kase, ans);
}
}