BZOJ2213[Poi2011]Difference——DP
题目描述
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.
一行,表示结果
样例输入
aabbaaabab
样例输出
Explanation of the example: The fragment that attains the difference of 3 in the number of occurrences of a and b is aaaba.
#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的更多相关文章
- 【BZOJ2213】[Poi2011]Difference DP
[BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...
- BZOJ2213: [Poi2011]Difference
2213: [Poi2011]Difference Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 343 Solved: 108[Submit][St ...
- BZOJ2213 [Poi2011]Difference 【乱搞】
题目链接 BZOJ2213 题解 考虑任意一对点的贡献,单独拿出那些点所在位置 一个设为\(1\),一个设为\(-1\),从头到尾扫一遍维护前缀和,以及当前最小前缀和 两者相减更新答案 需要注意的是当 ...
- bzoj2213: [Poi2011]Difference(思维题)
今天颓了一天T T 这题有两种写法... ①预处理出每种字符在原字符串中的位置,枚举两种字符作为最大值和最小值,把这两种字符的坐标归并排序,把最大值设为1,最小值设为-1,求最大子段和.注意因为 ...
- [bzoj2213][Poi2011]Difference_动态规划
Difference bzoj-2213 Poi-2011 题目大意:已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最 ...
- bzoj 2213: [Poi2011]Difference
Description A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We w ...
- POI2011题解
POI2011题解 2214先咕一会... [BZOJ2212][POI2011]Tree Rotations 线段树合并模板题. #include<cstdio> #include< ...
- POI做题笔记
POI2011 Conspiracy (2-SAT) Description \(n\leq 5000\) Solution 发现可拆点然后使用2-SAT做,由于特殊的关系,可以证明每次只能交换两个集 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- 【Codeforces 142C】Help Caretaker
Codeforces 142 C 题意:给一个\(n\times m\)的空矩阵,求里面放最多的可旋转的\(T\)字形的个数,并输出方案. 思路1: 由于\(n\)和\(m\)比较小,所以可以尝试搜索 ...
- UI自动化测试模型
所谓的自动化测试模型,可以理解为自动化测试框架+工具设计的一种思想产物. 先说说库.框架.工具之间的区别: 库:英文名Library,由代码集成的一个产品,供用户调用.面向对象的库叫做类库,面向过程的 ...
- linux调度器源码分析 - 新进程加入(三)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 之前的文章已经介绍了调度器已经初始化完成,现在只需要加入一个周期定时器tick驱动它进行周期调度即可,而加 ...
- AngularJs的ng-include的使用与实现
想在angularjs动态加载一个内容,我们可以使用ng-include来实现. 今天Insus.NET就在ASP.NET MVC环境中,举个例子来演示它的功能. 你可以在一个视图动态加载任一其它视图 ...
- b/s程序真的很方便部署吗
公共应用当然是web系统,这个不说,我说的是企业应用. 最近一些年在企业开发中都提倡web应用,仿佛winform可以结束了,但真的这样吗?最近几天的真实经历如下: 我们部门新开发了一套系统要上线,由 ...
- WPF中反转3D列表项
原文:WPF中反转3D列表项 WPF中反转3D列表项 周银辉记得在苹果电脑中有一个很酷的 ...
- TensorFlow入门(五)多层 LSTM 通俗易懂版
欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @creat_date: 2017-03-09 前言: 根据我本人学习 TensorFlow 实现 LSTM 的经 ...
- Java 大数、高精度模板
介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...
- [译]React 在服务端渲染的实现
原文地址:Server-Side React Rendering 原文作者:Roger Jin React 在服务端渲染的实现 React是最受欢迎的客户端 JavaScript 框架,但你知道吗(可 ...
- Mvc4_ @RenderBody、@RenderPage、@RenderSection用法
一.@RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合并,而新创建视图的内容会通过_Layout.cshtml布局页面的@RenderBody() ...