hdu 5583 Kingdom of Black and White(模拟,技巧)
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.
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 .
For every test case, you should output "Case #x: y",where x indicates the case number and counts from and y is the answer.
Case #:
Case #:
#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 ;
}
hdu 5583 Kingdom of Black and White(模拟,技巧)的更多相关文章
- HDU 5583 Kingdom of Black and White 水题
Kingdom of Black and White Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showpr ...
- hdu 5583 Kingdom of Black and White
Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5583 Kingdom of Black and White(暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...
- hdu-5583 Kingdom of Black and White(数学,贪心,暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 Kingdom of Black and White Time Limit: 2000/1000 ...
- HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...
- hdu 4948 Kingdom(推论)
hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...
- HDU5583 Kingdom of Black and White
Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- hdu 2629 Identity Card (字符串解析模拟题)
这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...
- [HDOJ5583]Kingdom of Black and White(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大.先分块,然后统计好原来的 ...
随机推荐
- shell基础——创建临时文件
(1)当前时间命名临时文件: 按照一定的格式获取当前时间:date +%y-%m-%d_%H:%M:%S #generate the file name tmpfile=`date +%y-%m-%d ...
- android客户端从服务器端获取json数据并解析的实现代码(重要)
首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exc ...
- [置顶] 如何把你的笔记本电脑变成一个Wi-Fi路由器在Windows 7 & 8?
翻译自:http://www.hakanakdag.net/windows/how-to-create-wireless-ad-hoc-internet-connection-in-windows-8 ...
- cf466C Number of Ways
C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 电子科大POJ "a^b"
a ^ b Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) C-sources: ...
- hdu 2157 How many ways_ 矩阵快速幂
题意:略 直接矩阵乘法就行了 #include <iostream> #include<cstdio> #include<cstring> using namesp ...
- Scrapy URLError
错误信息如下: 2015-12-03 16:05:08 [scrapy] INFO: Scrapy 1.0.3 started (bot: LabelCrawler) 2015-12-03 16:05 ...
- 谈谈 WLST Custom Commands
在了解WLST定制命令之前,简单说一下WLST,WLST 全称叫Weblogic Scripting Tool, 它提供了一组预定义命令来方便Weblogic的用户通过命令行对Weblogic 实例, ...
- 把DEDE的在线文本编辑器换成Kindeditor不显示问题
在织梦论坛下载了[Kindeditor编辑器For DedeCMS],按照操作说明安装后,后台文章编辑的区域却显示空白,有人说不兼容V57版本,有人说不兼容gbk版本,我也纠结了很久,在网上找了很多版 ...
- android 自定义圆形进度条
一.通过动画实现 定义res/anim/loading.xml如下: [html] view plain copy print ? <?xml version="1.0" ...