2213: [Poi2011]Difference

Time Limit: 10 Sec  Memory Limit: 32 MB
Submit: 343  Solved: 108
[Submit][Status]

Description

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的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最大。求这个个数。

Input

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

Output

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.

一行,表示结果

Sample Input

10
aabbaaabab

Sample Output

3
Explanation of the example: The fragment that attains the difference of 3 in the number of occurrences of a and b is aaaba.

HINT

 

Source

题解:

copy:

记f[i][j]表示第i位j字母出现的次数,则ans=max(f[i1][a]-f[i0][a]-(f[i1][b]-f[i0][b]))=max(f[i1][a]-f[i1][b]-(f[i0][a]-f[i0][b]))

所以维护一下f[i][a]-f[i][b]的最小值,更新的时候看一下。

连续一段区间只有一个字母:记录次小值,用上一次更新答案时的cnt[a]或者cnt[b]来判断即可。

其实这题和连续最大和差不多,我太sb了想不到。。。

代码理解起来好困难,看了好久stupid_lulu's 的代码没看懂,唉,挖个坑,以后来填

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 500+100
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,ans=,f[][],g[][];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)
{
char ch=getchar();
while(ch>'z'||ch<'a')ch=getchar();
int x=ch-'a';
for0(j,)
{
f[x][j]++;
f[j][x]--;
if(!g[j][x])g[j][x]=;
if(g[j][x]==){g[j][x]=;f[j][x]++;}
if(f[j][x]<=-){f[j][x]=-;g[j][x]=;}
if(g[j][x])ans=max(ans,f[j][x]);
if(g[x][j])ans=max(ans,f[x][j]);
}
}
printf("%d\n",ans);
return ;
}

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

  1. BZOJ2213[Poi2011]Difference——DP

    题目描述 A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We would li ...

  2. BZOJ2213 [Poi2011]Difference 【乱搞】

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

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

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

  4. 【BZOJ2213】[Poi2011]Difference DP

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

  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系列(5)

    正规.严谨.精妙. -POI BZOJ 2213 : [Poi2011]Difference 如果我们每次枚举两个字母最大最小情况时,很容易想到写出代码里注释的样子.这样是26*26*n的,我们发现枚 ...

  9. bzoj AC倒序

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

随机推荐

  1. tungsten开机启动及进程开启停止

  2. JFreeChart学习

    一.步骤: 创建数据集(准备数据) 根据数据集生成JFreeChart对象,并对其做相应的设置(标题,图例,x轴,Y轴,对象渲染等) 将JFreeChart对象输出到文件或者Servlet输出流等 二 ...

  3. eclipse tomcat 网页404的一个小问题

    之前一篇文章说过关于修改tomcat布置的应用的localhost路径.因为有两个项目在eclipse,所以我每次启动tomcat的时候都会加载两个项目, 但我其实只用调试其中一个项目,所以我就在se ...

  4. SpringMVC文件上传与下载

    一.关键步骤 ①引入核心JAR文件 SpringMVC实现文件上传,需要再添加两个jar包.一个是文件上传的jar包,一个是其所依赖的IO包.这两个jar包,均在Spring支持库的org.apach ...

  5. thread block grid

    grid里面包含block,block里面包含thread grid里面所有的block都是同样大小的,  每个block最多可以有1024个thread. blockDim表示一个block里面th ...

  6. android学习笔记---63-PopupWindow,泡泡窗口的实现

    转载http://blog.csdn.net/lidew521/article/details/8976627 PopupWindow是一个可以显示在当前Activity之上的浮动容器,PopupWi ...

  7. [Redux] Implementing combineReducers() from Scratch

    The combineReducers function we used in previous post: const todoApp = combineReducers({ todos, visi ...

  8. gcc和g++编译c或者c++文件碰到的问题

    gcc和g++都是GNU(组织)的一个编译器.        误区一:gcc只能编译c代码,g++只能编译c++代码      两者都可以,但是请注意:      1.后缀为.c的,gcc把它当作是C ...

  9. Windows server 2008系统下FTP服务器的安装

    一.在 Windows 服务器上安装 FTP 服务 1. 在"开始"菜单上,单击"管理工具",然后单击"服务器管理器". 2. 在" ...

  10. Python 类 --基础与要点

    1:2.x与3.x区别: 2.x:老式类,都继承自object,继承机制为深度优先 3.x:新式类不需要写(object),继承机制为广度优先 2:类说明: 在定义类下用""&qu ...