HDU 5583 Kingdom of Black and White(暴力)

时间:2023-03-09 03:19:58
HDU 5583 Kingdom of Black and White(暴力)

http://acm.hdu.edu.cn/showproblem.php?pid=5583

题意:

给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x*x,现在最多可以修改原串中的一个字符,问答案最大可以为多少。

思路:
暴力求解。

一开始只需要预处理分块,计算出每一分块的个数,然后暴力处理一下即可,需要注意分块数为1的情况,此时左右两边都要合并起来。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1e5+;
typedef long long ll; char s[maxn];
ll a[maxn];
int top; int main()
{
//freopen("in.txt","r",stdin);
int T;
int kase = ;
scanf("%d",&T);
while(T--)
{
memset(a,,sizeof(a));
scanf("%s",s);
int len = strlen(s);
ll cnt = ;
top = ;
ll tmp = ;
for(int i=;i<len;i++)
{
if(i== || s[i]==s[i-]) cnt++;
else
{
a[++top]=cnt;
tmp+=cnt*cnt;
cnt = ;
}
if(i==len-)
{
a[++top]=cnt;
tmp+=cnt*cnt;
}
}
ll ans = ;
for(int i=;i<=top;i++)
{
ll tt = tmp;
if(i!=)
{
tt=tt-a[i]*a[i]-a[i-]*a[i-];
if(a[i]==)
{
tt-=a[i+]*a[i+];
tt+=(a[i-]+a[i]+a[i+])*(a[i-]+a[i]+a[i+]);
}
else
{
tt+=(a[i-]+)*(a[i-]+)+(a[i]-)*(a[i]-);
}
ans=max(ans,tt);
}
tt = tmp;
if(i!=top)
{
tt=tt-a[i]*a[i]-a[i+]*a[i+];
if(a[i]==)
{
tt-=a[i-]*a[i-];
tt+=(a[i-]+a[i]+a[i+])*(a[i-]+a[i]+a[i+]);
}
else
{
tt+=(a[i]-)*(a[i]-)+(a[i+]+)*(a[i+]+);
}
ans=max(ans,tt);
}
}
if(top==) ans=a[]*a[];
printf("Case #%d: ",++kase);
cout<<ans<<endl;
}
return ;
}