Problem Description
In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog. Now N frogs are standing in a line, some of them are black, the others are white. The total strength of those frogs are calculated by dividing the line into minimum parts, each part should still be continuous, and can only contain one kind of frog. Then the strength is the sum of the squared length for each part. However, an old, evil witch comes, and tells the frogs that she will change the color of at most one frog and thus the strength of those frogs might change. The frogs wonder the maximum possible strength after the witch finishes her job.
Input
First line contains an integer T, which indicates the number of test cases. Every test case only contains a string with length N, including only (representing
a black frog) and (representing a white frog). ⋅ ≤T≤. ⋅ for % data, ≤N≤. ⋅ for % data, ≤N≤. ⋅ the string only contains and .
Output
For every test case, you should output "Case #x: y",where x indicates the case number and counts from and y is the answer.
Sample Input
Sample Output
Case #:
Case #:
Source
题意:例如000011,有连续为0的子序列长度为4,连续为1的子序列长度为2,所以这段字符串的价值为4*4+2*2=20,女巫最多可以将一个0或1改成1或0,那么把第5个字符1改成0,最后可以得到5*5+1*1=26,
例如0101,连续为0的子序列有2段,长度皆为1,连续为1的子序列也是两段,长度皆为1。当前价值为1*1*4=4,把第二个字符改0或者把第三个字符改1可以得到3*3+1*1=10,先离散化,求出结果,如果全部的颜色都一样则不用改变就是最优。总的来说是,先离散化,求出结果,如果全部的颜色都一样则不用改变就是最优。离散化之后,然后枚举每个离散块,尝试改变每个离散块两边的青蛙的颜色,更新最大值。
问题很多啊,开始是超时,原来把改变的那部分数算了就可以了,后来是答案错误,没用long long。
其他不难,主要是模拟各种情况,一开始要先离散化成各个模块,接下来就简单了。具体看代码。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
struct Node{
char c;
ll len;
}node[N];
char s[N];
int main()
{
int t;
ll ac=;
scanf("%I64d",&t);
while(t--){
for(ll i=;i<N;i++){
node[i].len=;
}
scanf("%s",s);
ll len=strlen(s);
ll k=;
node[k].c=s[];
node[k].len++;
ll now=;
for(ll i=;i<len;i++){
if(s[i]==s[now]){
node[k].len++;
now++;
}else{
k++;
node[k].c=s[i];
node[k].len++;
now++;
}
}
//for(ll i=0;i<=k;i++){
// prllf("%c %d\n",node[i].c,node[i].len);
//}
ll ans=;
for(ll i=;i<=k;i++){
ans+=node[i].len*node[i].len;
}
ll sum=ans; //prllf("%d\n",ans);
for(ll i=;i<=k;i++){
if(node[i].len==){ if(i==){
ll new_num=;
ll old_num=;
new_num=(node[].len+)*(node[].len+);
old_num=(node[].len)*(node[].len)+(node[].len)*(node[].len);
ans=max(ans,sum-old_num+new_num);
}else if(i==k){
ll new_num=;
ll old_num=;
new_num=(node[i-].len+)*(node[i-].len+);
old_num=(node[i].len)*(node[i].len)+(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num);
}else{
ll new_num=;
ll old_num=; new_num=(node[i-].len+)*(node[i-].len+);
old_num=(node[i].len)*(node[i].len)+(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num); new_num=;
old_num=; new_num=(node[i+].len+)*(node[i+].len+);
old_num=(node[i].len)*(node[i].len)+(node[i+].len)*(node[i+].len);
ans=max(ans,sum-old_num+new_num); new_num=;
old_num=;
if((node[i].c!=node[i-].c) && (node[i].c!=node[i+].c)){
new_num=(node[i-].len++node[i+].len)*(node[i-].len++node[i+].len);
old_num=(node[i].len)*(node[i].len)+(node[i+].len)*(node[i+].len)+(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num);
}
} }else{
if(i==){ ll new_num=;
ll old_num=;
new_num+=(node[].len-)*(node[].len-);
new_num+=(node[].len+)*(node[].len+); old_num+=(node[].len)*(node[].len);
old_num+=(node[].len)*(node[].len); ans=max(ans,sum-old_num+new_num); }else if(i==k){ ll new_num=;
ll old_num=;
new_num+=(node[k].len-)*(node[k].len-);
new_num+=(node[k-].len+)*(node[k-].len+); old_num+=(node[k].len)*(node[k].len);
old_num+=(node[k-].len)*(node[k-].len); ans=max(ans,sum-old_num+new_num); }else{ ll new_num=;
ll old_num=; new_num+=(node[i-].len+)*(node[i-].len+);
new_num+=(node[i].len-)*(node[i].len-);
old_num+=(node[i].len)*(node[i].len);
old_num+=(node[i-].len)*(node[i-].len);
ans=max(ans,sum-old_num+new_num); new_num=;
old_num=;
new_num+=(node[i].len-)*(node[i].len-);
new_num+=(node[i+].len+)*(node[i+].len+);
old_num+=(node[i].len)*(node[i].len);
old_num+=(node[i+].len)*(node[i+].len);
ans=max(ans,sum-old_num+new_num);
}
}
}
printf("Case #%I64d: ",++ac);
printf("%I64d\n",ans);
}
return ;
}