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. RocketMQ最佳实战

    RocketMQ 客户端最佳实践 1. Producer最佳实践 发送消息注意事项 1). 一个应用尽可能用一个Topic,消息子类型用tags来标识,tags可以由应用自由设置. 2). 消息发送成 ...

  2. android本地数据库,微信数据库WCDB for Android 使用实例

    android本地数据库,微信数据库WCDB for Android 使用实例 Home · Tencent/wcdb Wikihttps://github.com/Tencent/wcdb/wiki ...

  3. linux内核的双链表list_head、散列表hlist_head

    一.双链表list_head 1.基本概念 linux内核提供的标准链表可用于将任何类型的数据结构彼此链接起来. 不是数据内嵌到链表中,而是把链表内嵌到数据对象中. 即:加入链表的数据结构必须包含一个 ...

  4. easyui dialog 和 dategrid相关设置与应用

    1 dialog一些参数   可以进行文件上传等操作closed:true:定义初始状态为关闭:modal:true:对话框被打开时,会有一个modal-mask,使得对话框底部的内容被一个层覆盖,不 ...

  5. 怎样从外网访问内网WampServer?

    本地安装了一个WampServer,只能在局域网内访问,怎样从外网也能访问到本地的WampServer呢?本文将介绍具体的实现步骤. 准备工作 安装并启动WampServer 默认安装的WampSer ...

  6. 自定义schema 流程

    参考 https://www.cnblogs.com/googlemeoften/p/5746684.html

  7. Caused by: java.sql.SQLException: Field 'category_id' doesn't have a default value

    ### The error may involve com.qingmu.core.dao.ad.ContentDao.insertSelective-Inline ### The error occ ...

  8. spring框架入门之一

    一.什么是Spring框架 1.什么是Spring Spring框架是个一个全栈的框架.意思就是使用Spring的框架可以开发web层,service层还有dao层. 本质:Spring就是一个对象的 ...

  9. linux 搭建svn(待完成)

    http://blog.csdn.net/lazy_cc/article/details/8726500搭建仓库 http://blog.csdn.net/xocoder/article/detail ...

  10. django 动态生成PDF文件

    可以通过开源的Python PDF库ReportLab来实现PDF文件的动态生成. 一.安装ReportLab ReportLab库在PyPI上提供,可以使用pip来安装: $ pip install ...