2015上海区域赛 K题
#Description
给一串0和1,然后你可以变一个
这样就分成了若干块
求然后每块长度的平方和最大
#Algorithm
两种情况
1.长度大于1
这样变两头
2.长度=1,这样3段就练成一段
#Hint
貌似必须用vector,不然超时?
#Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
string s;
void solve()
{
vector<long long> a;
cin>>s;
a.push_back(0);
a.push_back(1);
long long ans0 = 0;
for (int i = 1; i < s.size(); i++)
if (s[i] == s[i - 1])
a[a.size() - 1]++;
else
{
a.push_back(1);
}
a.push_back(0);
for (int i = 0; i < a.size(); i++)
ans0 += (a[i] * a[i]);
long long ans = ans0;
for (int i = 1; i < a.size() - 1; i++)
if (a[i] > 1)
{
long long t = ans0 + (a[i] - 1) * (a[i] - 1) + (a[i + 1] + 1) * (a[i + 1] + 1) - a[i] * a[i] - a[i + 1] * a[i + 1];
if (t > ans) ans = t;
t = ans0 + (a[i] - 1) * (a[i] - 1) + (a[i - 1] + 1) * (a[i - 1] + 1) - a[i] * a[i] - a[i - 1] * a[i - 1];
if (t > ans) ans = t;
}else
{
long long t = ans0 + (a[i - 1] + 1 + a[i + 1]) * (a[i - 1] + 1 + a[i + 1]) - (a[i - 1] * a[i - 1] + a[i] * a[i] + a[i + 1] * a[i + 1]);
if (t > ans) ans = t;
}
cout << ans << endl;
}
int main()
{
// freopen("input.txt", "r", stdin);
int n;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++)
{
printf("Case #%d: ", i);
solve();
}
}