标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1890 Accepted Submission(s): 588
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w",stdout);
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int,int> PII;
const int MAXN = 1e5 + 5;
int T;
char a[MAXN];
LL dp[MAXN];
LL b[MAXN];
int main()
{
//FIN
scanf("%d", &T);
int cas = 1;
while(T--) {
scanf("%s", a);
int lena = strlen(a);
dp[0] = 1;
LL sum = 0;
int t = 0;
LL mx = 0;
memset(b, 0, sizeof(b));
for(int i = 1; i < lena; i++) {
if(a[i] == a[i - 1]) dp[i] = dp[i - 1] + 1;
else {
mx = max(dp[i - 1], mx);
b[t++] = dp[i - 1];
sum += LL(dp[i - 1] * dp[i - 1]);
dp[i] = 1;
}
}
b[t] = dp[lena - 1];
mx = max(b[t], mx);
sum += LL(b[t] * b[t]);
LL ans = sum;
if(b[0] == lena) {
ans = sum;
printf("Case #%d: %lld\n", cas++, ans);
continue;
}
LL tt;
//cout << "mx=" << mx << endl;
for(int i = 0; i <= t; i++) {
if(b[i] == mx) {
//cout << "i=" << i << endl;
if(i == 0) tt = b[1];
else if(i == t) tt = b[t - 1];
else tt = min(b[i - 1], b[i + 1]);
LL now = sum + (mx + 1) * (mx + 1) - (mx * mx) + (tt - 1) * (tt -1) - tt * tt;
ans = max(ans, now);
}
}
for(int i = 0; i <= t; i++) {
if(b[i] == 1) {
LL now = sum - b[i - 1] * b[i - 1] - b[i + 1] * b[i + 1] - 1 + (b[i + 1] + 1 + b[i - 1]) * (b[i + 1] + 1 + b[i - 1]);
ans = max(ans, now);
}
}
printf("Case #%d: %lld\n", cas++, ans);
}
return 0;
}
HDU 5583 Kingdom of Black and White 暴力
标签:
原文地址:http://www.cnblogs.com/Hyouka/p/5905675.html