#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define In inline
#define PI atan(1.0)*4
#define enter puts("")
#define MaxN 0x3f3f3f3f
#define MinN 0xc0c0c0c0
#define bug(x) cerr<<#x<<'='<<x<<' '
#define ALL(a) a.begin(), a.end()
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define mset(a,b) memset(a,b,sizeof(a))
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
typedef pair<int,int> pii;
In ll read()
{
ll ans = 0;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x%10 + '0');
}
ll max(ll a, ll b){
if(a > b) return a;
return b;
}
const int N = 100010;
int t;
ll a[N];
int main()
{
t = read();
rep(kcase, 1, t){
mset(a, 0);
string s;
cin >> s;
printf("Case #%d: ", kcase);
int cnt = 1, idx = 1;
bool check = 0;//记录是否有1出现
rep(i, 1, s.length()-1)
if(s[i] == s[i-1]) cnt ++;
else{
a[idx ++] = cnt;
if(cnt == 1) check = 1;
cnt = 1;
}
if(cnt == 1) check = 1;
a[idx] = cnt;//记录初始字符串中每个序列的长度
//rep(i, 1, idx) cout << a[i] << " ";
//enter;
ll res = a[1]*a[1];//防止数组越界,先加上a[1]的平方
ll max_ = 0;//max_为只考虑对两个大于1片段操作增加的最大值,max_的推导(x+1)*(x+1)-x*x+(y-1)*(y-1)-y*y ==> 2*(x-y+1);
rep(i, 2, idx){
res += (ll)a[i]*a[i];
max_ = max(max_, 2*(a[i-1]-a[i]+1));
}
res += max_;
if(check){//有1;
ll max_around = 0;//(a+b+c)*(a+b+c)-a*a-b*b-c*c ==> 2*(a+c+a*c) ,b=1;
ll _res = 0;
rep(i, 1, idx)
if(a[i] == 1)
if(2*(a[i-1]+a[i+1]+a[i-1]*a[i+1]) > max_around) max_around = 2*(a[i-1]+a[i+1]+a[i-1]*a[i+1]);
rep(i, 1, idx)
_res += (ll)a[i]*a[i];
_res += max_around;
if(_res > res) res = _res;
}
cout << res << "\n";
}
return 0;
}
2015ACM/ICPC亚洲区上海站 K-Kingdom of Black and White HDU-5583
最新推荐文章于 2024-11-05 21:58:11 发布