BZOJ2213: [Poi2011]Difference
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
aabbaaabab
Sample Output
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的更多相关文章
- BZOJ2213[Poi2011]Difference——DP
题目描述 A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We would li ...
- BZOJ2213 [Poi2011]Difference 【乱搞】
题目链接 BZOJ2213 题解 考虑任意一对点的贡献,单独拿出那些点所在位置 一个设为\(1\),一个设为\(-1\),从头到尾扫一遍维护前缀和,以及当前最小前缀和 两者相减更新答案 需要注意的是当 ...
- bzoj2213: [Poi2011]Difference(思维题)
今天颓了一天T T 这题有两种写法... ①预处理出每种字符在原字符串中的位置,枚举两种字符作为最大值和最小值,把这两种字符的坐标归并排序,把最大值设为1,最小值设为-1,求最大子段和.注意因为 ...
- 【BZOJ2213】[Poi2011]Difference DP
[BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...
- [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系列(5)
正规.严谨.精妙. -POI BZOJ 2213 : [Poi2011]Difference 如果我们每次枚举两个字母最大最小情况时,很容易想到写出代码里注释的样子.这样是26*26*n的,我们发现枚 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- Mac下Sublime Text 3安装配置
1.下载Sublime Text 3 2.安装Package Control,参考 快捷键 control + ` 或者菜单栏选择View > Show Console 在控制台输入: impo ...
- spring读取properties文件
1.方式一 <util:properties id="meta" location="classpath:config/metainfo.properties&qu ...
- android 初学: 提示No Launcher activity found!
提示No Launcher activity found! 三步检查: 1 必须有 <category android:name="android.intent.category.LA ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...
- iOS蓝牙4.0协议简单介绍
iOS开发蓝牙4.0的框架是CoreBluetooth,本文主要介绍CoreBluetooth的使用,关于本文中的代码片段大多来自github上的一个demo,地址是myz1104/Bluetooth ...
- zend_db连接mysql(附完整代码)(转)
在看这些之前请确保你正确加载了PDO扩展. 作法是编辑php.ini手动增加下面这两行(前面要没有分号;):extension=php_pdo.dllextension=php_pdo_mysql.d ...
- Factorization Machines 学习笔记(三)回归和分类
近期学习了一种叫做 Factorization Machines(简称 FM)的算法,它可对随意的实值向量进行预測.其主要长处包含: 1) 可用于高度稀疏数据场景:2) 具有线性的计算复杂度.本文 ...
- 详解C++ friend关键字
1. 为什么要使用友元? 通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为 public( 共用的) ,然而这做带来的问题遍是任何外部函数都可以毫无约束的访 ...
- spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)
作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息. 因为它实现了ServletContextListener这个接口,在web.xml配置这个监听 ...