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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using std::max;
  5. using std::min;
  6. const int M=1e6+,inf=0x3f3f3f3f;
  7. int read(){
  8. int ans=,f=,c=getchar();
  9. while(c<''||c>''){if(c=='-') f=-; c=getchar();}
  10. while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
  11. return ans*f;
  12. }
  13. char c[M];
  14. int ans,n,f[][],s[],last[],p[][];
  15. int main(){
  16. n=read(); scanf("%s",c+);
  17. for(int i=;i<=n;i++){
  18. int now=c[i]-'a';
  19. s[now]++; last[now]=i;
  20. for(int j=;j<;j++){
  21. if(j!=now&&s[j]) ans=max(ans,(s[now]-s[j])-f[now][j]-(p[now][j]==last[j]));
  22. if(j!=now&&s[j]) ans=max(ans,(s[j]-s[now])-f[j][now]-(p[j][now]==last[now]));
  23. }
  24. for(int j=;j<;j++){
  25. if(f[j][now]>s[j]-s[now]) f[j][now]=s[j]-s[now],p[j][now]=i;
  26. if(f[now][j]>s[now]-s[j]) f[now][j]=s[now]-s[j],p[now][j]=i;
  27. }
  28. }
  29. printf("%d\n",ans);
  30. return ;
  31. }

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. emacs编译整个emacs.d目录

    $emacs 在emacs查看里面,输入: C-u M-x byte-recompile-directory 然后输入 ~/.emacs.d 即可.

  2. CoordinatdBolt原理分析

    参考链接:http://xumingming.sinaapp.com/811/twitter-storm-code-analysis-coordinated-bolt/ CoordinatedBolt ...

  3. JavaScript设计模式学习之路——面向对象的思想

    今天,我拿到了张容铭写的这本<JavaScript设计模式>这本书,开始了关于JavaScript更深一点的学习. 看到这本书开始的时候,虽然之前通过看书.一些比较好的视频的讲解,对Jav ...

  4. docker-py execute echo无效

    错误写法: cli.execute('9b2606a50304','echo "bibo">/tmp/1.txt')   争取写法: cli.execute('9b2606a ...

  5. influxdb 命令

    写入数据: curl -X POST -d '[{"name":"foo","columns":["val"],&quo ...

  6. 九度-题目1026:又一版 A+B

    http://ac.jobdu.com/problem.php?pid=1026 题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m ...

  7. BZOJ 2957 楼房重建(线段树区间合并)

    一个显而易见的结论是,这种数字的值是单调递增的.我们修改一个数只会对这个数后面的数造成影响.考虑线段树划分出来的若干线段. 这里有两种情况: 1.某个线段中的最大值小于等于修改的数,那么这个线段的贡献 ...

  8. General Sultan UVA - 11604(建图暴力)

    给出n个字符串,询问是否存在一个字符串(可以是给出的几个中的 也可以是组合成的),使得用字符串(随便你用多少个)来拼凑这个串,能够至少有两种拼法 解析: 把每一个字符串的每一个位置的字符看作结点,进行 ...

  9. Linux内核分析5

    周子轩 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.学习总结 通过gdb ...

  10. HikariPool-1 - Connection is not available, request timed out after XXXXms.

    hikaripool-0-连接不可用,请求在30000ms之后超时.意思是池等待30000ms的免费连接,但是您的应用程序没有返回任何连接. 原因一:连接泄漏(在从池中借用之后连接没有关闭).解决方法 ...