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.
————————————————————————————————————————————
这道题我们从1扫到n 每次经过一个位置 只会改变一个字母的出现次数
我们可以把

转换成

我们用s[i]表示i这个字母的出现次数  f[i][j]表示前面某一时刻s[i]-s[j]的最小值

p[i][j]表示最小值转移时候的位置 last[i]表示i上次出现的位置

这样就完美解决答案了辣2333

#include<cstdio>
#include<cstring>
#include<algorithm>
using std::max;
using std::min;
const int M=1e6+,inf=0x3f3f3f3f;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
char c[M];
int ans,n,f[][],s[],last[],p[][];
int main(){
n=read(); scanf("%s",c+);
for(int i=;i<=n;i++){
int now=c[i]-'a';
s[now]++; last[now]=i;
for(int j=;j<;j++){
if(j!=now&&s[j]) ans=max(ans,(s[now]-s[j])-f[now][j]-(p[now][j]==last[j]));
if(j!=now&&s[j]) ans=max(ans,(s[j]-s[now])-f[j][now]-(p[j][now]==last[now]));
}
for(int j=;j<;j++){
if(f[j][now]>s[j]-s[now]) f[j][now]=s[j]-s[now],p[j][now]=i;
if(f[now][j]>s[now]-s[j]) f[now][j]=s[now]-s[j],p[now][j]=i;
}
}
printf("%d\n",ans);
return ;
}

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

  1. BZOJ2213: [Poi2011]Difference

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

  2. BZOJ 2530 Poi2011 Party 【枚举】

    BZOJ 2530 Poi2011 Party Description Byteasar intends to throw up a party. Naturally, he would like i ...

  3. 【BZOJ2213】[Poi2011]Difference DP

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

  4. [bzoj 2216] [Poi2011] Lightning Conductor

    [bzoj 2216] [Poi2011] Lightning Conductor Description 已知一个长度为n的序列a1,a2,-,an. 对于每个1<=i<=n,找到最小的 ...

  5. 洛谷 3519 && bzoj 2213 Difference

    联考考试考到了这个题,随机化40分,现在来秒掉它吧. 题意: 给一个字符串,求其中的一段,使得出现次数最多的字符与出现次数最少的字符的出现次数之差最大. 输入输出样例 输入样例#1: 复制 10 aa ...

  6. [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

  7. [BZOJ 2350] [Poi2011] Party 【Special】

    题目链接: BZOJ - 2350 题目分析 因为存在一个 2/3 n 大小的团,所以不在这个团中的点最多 1/3 n 个. 牺牲一些团内的点,每次让一个团内的点与一个不在团内的点抵消删除,最多牺牲 ...

  8. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

  9. bzoj 2217 [Poi2011]Lollipop 乱搞 贪心

    2217: [Poi2011]Lollipop Time Limit: 15 Sec  Memory Limit: 64 MBSec  Special JudgeSubmit: 383  Solved ...

随机推荐

  1. Iterable,Iterator和forEach

    Iterable Interface Iterable<T> 方法: Iterator<T> iterator() Returns an iterator over a set ...

  2. (九) 使用Jmeter 做分布式压测 ;

    在使用Jmeter进行性能测试时,如果并发数比较大(比如最近项目需要支持1000并发),单台电脑的配置(CPU和内存)可能无法支持,这时可以使用Jmeter提供的分布式测试的功能. 一.Jmeter分 ...

  3. php奇葩错误:htmlspecialchars处理中文丢失

    $value = "中文中文"; $res = htmlspecialchars($value); 经过这个函数处理之后,$res就直接变成了空的字符串. 奇葩错误啊!后来发现要这 ...

  4. js & right click menu

    js & right click menu https://stackoverflow.com/questions/4909167/how-to-add-a-custom-right-clic ...

  5. Monkey自动化测试

    Monkey简介 语法参数 实际应用 一.Monkey简介 1.什么是Monkey? 基于健壮性.稳定性的考虑:如果将一个应用交给一个人长时间不停地乱点乱按,程序会怎么样? 有时候运行相同系列的测试, ...

  6. 【uoj#139】[UER #4]被删除的黑白树 贪心

    题目描述 给出一个 $n$ 个节点的树,$1$ 号点为根.现要将其中一些点染成黑色,使得每个叶子节点(不包括根节点)到根节点路径上的黑点数相同.求最多能够染多少个黑点. 题解 贪心 显然有结论:选择的 ...

  7. 【三】shiro入门 之 Realm

    Realm:域,Shiro 从从Realm获取安全数据(如用户.角色.权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法:也 ...

  8. [AT2364] [agc012_d] Colorful Balls

    题目链接 AtCoder:https://agc012.contest.atcoder.jp/tasks/agc012_d 洛谷:https://www.luogu.org/problemnew/sh ...

  9. 代码收藏系列--javascript--移动端技巧

    JS判断是否是手机端访问: var is_mobi = navigator.userAgent.toLowerCase().match(/(ipod|iphone|android|coolpad|mm ...

  10. 如何在Linux系统上安装QQ

    转载自KKK博客 首先我们需要在wine的官网上安装一个wine. https://wiki.winehq.org/Ubuntu_zhcn 按照上面的提示一步步来,做完之后点下面的链接下载 https ...