题目描述

A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We would like to choose a non-empty contiguous (i.e. one-piece) fragment of the word so as to maximise the difference in the number of occurrences of the most and the least frequent letter in the fragment. We are assuming that the least frequent letter has to occur at least once in the resulting fragment. In particular, should the fragment contain occurrences of only one letter, then the most and the least frequent letter in it coincide.

已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最大。求这个个数。

输入

The first line of the standard input holds one integer (1<=N<=1000000) that denotes the length of the word. The second line holds a word consisting of lower-case letters of the English alphabet.

第一行,n
第二行,该字符串
1<=n<=1000000

输出

The first and only line of the standard output is to hold a single integer, equal to the maximum difference in the number of occurrences of the most and the least frequent letter that is attained in some non-empty contiguous fragment of the input word.

一行,表示结果

样例输入

10
aabbaaabab

样例输出

3
Explanation of the example: The fragment that attains the difference of 3 in the number of occurrences of a and b is aaaba.
 
一道很好的思维题。
假设最后答案的那一段中最多的是a,最少的是b,那么答案就是(ra-la)-(rb-lb),其中l,r代表序列端点处的前缀和,变换之后也就是(ra-rb)+(lb-la),也就是求端点位置前缀和之差。我们设f[i][j]表示当前位置之前的某一位置i字符个数减j字符个数的最大值,g[i][j]表示当前位置i字符个数减j字符个数的值。因为每多一个位置只有一种字符的个数被改变,如果对答案有影响,那么当前位字符一定是答案中的那个最大值或者最小值。枚举其他25个字符,考虑当前位为最大值枚举字符为最小值或当前位为最小值枚举字符为最大值时对答案的影响来更新答案即可。时间复杂度O(26n)。
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char a[1000000];
int s[30];
int n;
int ans;
int f[30][30];
int g[30][30];
int main()
{
scanf("%d",&n);
scanf("%s",a+1);
memset(f,0xef,sizeof(f));
for(int i=1;i<=n;i++)
{
int x=a[i]-'a'+1;
s[x]++;
for(int j=1;j<=26;j++)
{
if(x!=j)
{
f[x][j]=max(f[x][j],g[x][j]);
g[x][j]=s[x]-s[j];
ans=max(ans,max(s[x]-s[j]+f[j][x],s[j]-s[x]+f[x][j]));
}
}
}
printf("%d",ans);
}

BZOJ2213[Poi2011]Difference——DP的更多相关文章

  1. 【BZOJ2213】[Poi2011]Difference DP

    [BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...

  2. BZOJ2213: [Poi2011]Difference

    2213: [Poi2011]Difference Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 343  Solved: 108[Submit][St ...

  3. BZOJ2213 [Poi2011]Difference 【乱搞】

    题目链接 BZOJ2213 题解 考虑任意一对点的贡献,单独拿出那些点所在位置 一个设为\(1\),一个设为\(-1\),从头到尾扫一遍维护前缀和,以及当前最小前缀和 两者相减更新答案 需要注意的是当 ...

  4. bzoj2213: [Poi2011]Difference(思维题)

       今天颓了一天T T 这题有两种写法... ①预处理出每种字符在原字符串中的位置,枚举两种字符作为最大值和最小值,把这两种字符的坐标归并排序,把最大值设为1,最小值设为-1,求最大子段和.注意因为 ...

  5. [bzoj2213][Poi2011]Difference_动态规划

    Difference bzoj-2213 Poi-2011 题目大意:已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最 ...

  6. bzoj 2213: [Poi2011]Difference

    Description A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We w ...

  7. POI2011题解

    POI2011题解 2214先咕一会... [BZOJ2212][POI2011]Tree Rotations 线段树合并模板题. #include<cstdio> #include< ...

  8. POI做题笔记

    POI2011 Conspiracy (2-SAT) Description \(n\leq 5000\) Solution 发现可拆点然后使用2-SAT做,由于特殊的关系,可以证明每次只能交换两个集 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. 【C语言】结构体占用字节数及存储与空间分配

    我们都知道在数据类型中,char类型占1个字节,short占2个字节,int占4个字节,long占8个字节等等. 在计算结构体大小时需要考虑其内存布局,结构体在内存中存放是按单元存放的,每个单元多大取 ...

  2. 从0移植uboot (一) _配置分析

    来源:Linux社区  作者:xiaojiang1025  :http://www.linuxidc.com/Linux/2017-02/141018.htm 和绝大多数源码编译安装一样,uboot的 ...

  3. ASP.NET MVC学习笔记(二)笔记

    接下来我们一起了解ASP.NET MVC的最重要的核心技术,了解ASP.NET MVC的开发框架,生命周期,技术细节. 一.Routing与ASP.NET MVC生命周期 1.Routing——网址路 ...

  4. mvn打包到私服的命令

    1.mvn clean package install -Dmaven.test.skip=true deploy 2.docker清楚Nexus私服上包的命令: a) docker exec -it ...

  5. CF1097F Alex and a TV Show 莫比乌斯反演、bitset

    传送门 发现自己对mobius反演的理解比较浅显-- 首先我们只需要维护每一个数的出现次数\(\mod 2\)的值,那么实际上我们只需要使用\(bitset\)进行维护,每一次加入一个数将其对应次数异 ...

  6. Sql_索引分析

    「索引就像书的目录, 通过书的目录就准确的定位到了书籍具体的内容」,这句话描述的非常正确, 但就像脱了裤子放屁,说了跟没说一样,通过目录查找书的内容自然是要比一页一页的翻书找来的快,同样使用的索引的人 ...

  7. React.js 入门与实战之开发适配PC端及移动端新闻头条平台课程上线了

    原文发表于我的技术博客 我在慕课网的「React.js 入门与实战之开发适配PC端及移动端新闻头条平台」课程已经上线了,文章中是目前整个课程的大纲,以后此课程还会保持持续更新,此大纲文档也会保持更新, ...

  8. TomCat 再次发布我的程序

    打包成.war的步骤就不说了,之后的配置和上一次的不一样. 在Tomcat的conf下的server.xml文件中,重新配置如下 <Service name="xfwweb" ...

  9. centos7下/etc/rc.local文件里配置的开机启动项不执行的解决办法

    习惯于在/etc/rc.local文件里配置我们需要开机启动的服务,这个在centos6系统下是正常生效的.但是到了centos7系统下,发现/etc/rc.local文件里的开机启动项不执行了!仔细 ...

  10. LVM基础详细说明及动态扩容lvm逻辑卷的操作记录

    LVM概念:---------------------------------------------------------------------------------------------- ...