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. 安卓apk的编译与反编译

    原文:https://blog.csdn.net/baidu_33870664/article/details/80186945 android基于java的,而java反编译工具很强悍,所以对正常a ...

  2. mycat水平分表

    和垂直分库不同,水平分表,是将那些io频繁,且数据量大的表进行水平切分. 基本的配置和垂直分库一样,我们需要改的就是我们的 schema.xml和rule.xml文件配置(server.xml不用做任 ...

  3. deepin安装Python3.6和pip

    1.安装python3.6 sudo apt-get install python3.6 2.修改软连接 sudo ln -s /usr/local/bin/python3.6 /usr/bin/py ...

  4. sitemap index

    https://docs.djangoproject.com/en/2.1/ref/contrib/sitemaps/ very good

  5. oracle 9i/10gR2所有版本下载地址

    Oracle 9i Oracle9i Database Release 2 Enterprise/Standard/Personal Edition for Windows NT/2000/XP ht ...

  6. windows下常用linux对应工具

    tail 可从http://files.cnblogs.com/hantianwei/tail.zip下载,解压后exe,如下: e:\>tail -fn 300 tool-slow.logus ...

  7. Angular 请求数据

    Angular 请求数据 get post 以及 jsonp 请求数据 引入 HttpModule .JsonpModule 普通的 HTTP 调用并不需要用到 JsonpModule,不过稍后我们就 ...

  8. yum指定安装目录

    纯粹做笔记 yum -c /etc/yum.conf --installroot=/opt/test/ --releasever=/ install lrzsz

  9. Codeforces Round #479 (Div. 3)题解

    CF首次推出div3给我这种辣鸡做,当然得写份博客纪念下 A. Wrong Subtraction time limit per test 1 second memory limit per test ...

  10. CentOS6.8下安装Redis

    1.由于Redis是使用C语言开发的,安装时需要对Redis的源码进行编译,编译依赖gcc环境,如果没有gcc,需要先安装gcc: yum install gcc-c++ 2.安装完成后,进入Redi ...