题目描述

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. 用Qt程序打开.txt 文件的时候,出现乱码的情况

    打开*.txt 文件出现乱码的情况,说明编码格式不对,一般的Windows下创建的txt 文件,到ubuntu 系统中打开会出现乱码的情况, 下面的代码读取文件,并且转化编码格式 void MainW ...

  2. ubuntu14.04下播放器SMplayer的安装

    1. Mplayer 与 SMplayer的区别 虽然MPlayer播放器是人类史上最强大的播放器(参数超过千个),但是其默认编译没有界面,所以写参数时间甚至比看片时间还长.虽然编译时候可以选择--e ...

  3. redis make jemalloc

    zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directoryzmalloc.h:55:2: error: #error ...

  4. Matplotlib 简单图例

    图例参考:http://matplotlib.org/gallery.html API参考:http://matplotlib.org/api/pyplot_summary.html # -*- co ...

  5. chrome浏览器直接打印 - z

    在地址栏敲: about:flags ,打开设置界面:停用:Enable Print Preview Registration PromosChrome快捷方式增加:--kiosk-printing这 ...

  6. SQL查询今天、昨天、7天内、30天 - 转

    今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:select * from 表名 where ...

  7. [Python]Python Class 中的 函数定义中的 self

    In [80]: class MyClass001: ....: def selfDemo(self): ....: print 'My Demo' ....: In [81]: p = MyClas ...

  8. 面试3——java集合类总结(Map)

    1.概述: Java 中的map集合使用键值对(key-value)来保持数据,其中值(value)可以重复,键(key)必须唯一,但最多只能有一个key为空,它的主要实现类有HashMap.Hash ...

  9. 在Windows7上如何找到Cookie

    摘要 出于兴趣爱好,前一阵子做了一个网页,网页中需要用到Cookie,但是,根据书上的说明,并没有找打教材中所说的Cookie的位置,本文就主要介绍在计算机(Win7)中Cookie的存放位置,同样适 ...

  10. js类型----你所不知道的JavaScript系列(5)

    ECMAScirpt 变量有两种不同的数据类型:基本类型,引用类型.也有其他的叫法,比如原始类型和对象类型等. 1.内置类型 JavaScript 有七种内置类型: • 空值(null) • 未定义( ...