2015ACM/ICPC亚洲区上海站 K-Kingdom of Black and White HDU-5583

题目链接

#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

marx97 ٩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值