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

HDU 5583 Kingdom of Black and White(暴力)的更多相关文章

  1. 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 ...

  2. hdu 5583 Kingdom of Black and White(模拟,技巧)

    Problem Description In the Kingdom of Black and White (KBW), there are two kinds of frogs: black fro ...

  3. 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 ...

  4. [HDOJ5583]Kingdom of Black and White(暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 一个01串,求修改一个位置,使得所有数均为0或1的子串长度的平方和最大.先分块,然后统计好原来的 ...

  5. 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 ...

  6. hdu 4948 Kingdom(推论)

    hdu 4948 Kingdom(推论) 传送门 题意: 题目问从一个城市u到一个新的城市v的必要条件是存在 以下两种路径之一 u --> v u --> w -->v 询问任意一种 ...

  7. HDU5583 Kingdom of Black and White

    Kingdom of Black and White Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  8. hdu 4740 The Donkey of Gui Zhou(暴力搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...

  9. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. linux常用命令:ls命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写,缺省下ls用来打印出当前目录的清单,如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linux ...

  2. linux下卸载mysql(rpm)

    linux下卸载mysql 查看是否安装了mysql的组件 rpm –qa |grep –I mysql 卸载前关闭mysql服务 service mysql status service mysql ...

  3. Linux基础命令---显示登录用户logname

    logname 显示当前登录的用户名称.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法        logname   2 ...

  4. Bluetooth_FTP_SPEC: 蓝牙FTP介绍

    FTP(Bluetooth File Transfer Profile) defines howfolders and files on a server device can be browsed ...

  5. CRM 权限设置 ss

    表结构的设计 权限表 url -url地址的正则表达式 ^$ title - 标题 角色表 name - 角色名称 permissions 多对多关联权限表 (权限和角色的关系表) 用户表 name ...

  6. getElementsByClassName方法的封装

    Element.prototype.getElementsByClassName = function(searchClass,node,tag){ if(document.getElementsBy ...

  7. css显示display、可见性visibility、定位position、对齐

    隐藏一个元素可以通过把display属性设置为"none",或把visibility属性设置为"hidden",但是这两种方法会产生不同的结果. display ...

  8. Getting started with Kaggle -- Kaggle Competitions

    1: The Competition We'll be learning how to generate a submission for a Kaggle competition. Kaggle i ...

  9. install docker swarm on centos

    ref: https://sonnguyen.ws/install-docker-docker-swarm-centos7/ https://hostadvice.com/how-to/how-to- ...

  10. Linux使用退格键时出现^H + Tab键命令补全失效/方向键失效 + ls文件夹和文件没有颜色

    删除问题 安装kalilinux使用普通用户的的时候按退格键无法实现删除功能 解决的办法有两个 一改变快捷键: 使用Ctrl+Backspace组合键可以实现删除功能 ctrl + backspace ...