【BZOJ2213】[Poi2011]Difference DP
【BZOJ2213】[Poi2011]Difference
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.
题解:我承认我做的可能有点麻烦~
先枚举出现次数最少的字符,然后设f[i][0]表示以i结束的最长一段区间使得(位置为i的字符出现次数-最少的字符出现的次数)最大,不强制要求区间中必须出现过次数最少的字符,f[i][1]表示强制区间中必须出现过次数最少的字符,然后DP搞一搞
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=1000010;
int n,ans;
int pre[maxn],s[maxn],f[maxn][2],head[30];
char str[maxn];
void calc(int cn)
{
int i;
for(i=1;i<=n;i++) s[i]=s[i-1]+(str[i]=='a'+cn);
if(!s[n]) return ;
f[0][0]=0,f[0][1]=-1<<30;
for(i=1;i<=n;i++)
{
if(str[i]=='a'+cn) continue;
f[i][0]=max(f[pre[i]][0]+1-s[i]+s[pre[i]],1);
f[i][1]=(s[i]>s[pre[i]])?max(f[pre[i]][0]+1-s[i]+s[pre[i]],0):(f[pre[i]][1]+1);
ans=max(ans,f[i][1]);
if(s[n]-s[i]) ans=max(ans,f[i][0]-1);
}
}
int main()
{
scanf("%d%s",&n,str+1);
int i;
for(i=1;i<=n;i++) pre[i]=head[str[i]-'a'],head[str[i]-'a']=i;
for(i=0;i<26;i++) calc(i);
printf("%d",ans);
return 0;
}
【BZOJ2213】[Poi2011]Difference DP的更多相关文章
- 【BZOJ2525】[Poi2011]Dynamite 二分+树形DP
[BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...
- 【BZOJ2525】[Poi2011]Dynamite(二分,树形dp)
[BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...
- 【BZOJ2217】[Poi2011]Lollipop 乱搞
[BZOJ2217][Poi2011]Lollipop Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T& ...
- 【LG3527】[POI2011]MET-Meteors
[LG3527][POI2011]MET-Meteors 题面 洛谷 题解 整体二分. 每次二分\(mid\),如果到时间\(mid\)以收集过\(P_i\)就存入子序列\(L\),否则存入子序列\( ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- 【BZOJ2527】[Poi2011]Meteors 整体二分
[BZOJ2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...
- 【BZOJ2530】[Poi2011]Party (xia)构造
[BZOJ2530][Poi2011]Party Description 给定一张N(保证N是3的倍数)个节点M条边的图,并且保证该图存在一个大小至少为2N/3的团. 请输出该图的任意一个大小为N/3 ...
- 【题解】POJ1934 Trip (DP+记录方案)
[题解]POJ1934 Trip (DP+记录方案) 题意: 传送门 刚开始我是这么设状态的(谁叫我DP没学好) \(dp(i,j)\)表示钦定选择\(i\)和\(j\)的LCS,然而你会发现这样钦定 ...
- 【BZOJ2216】[Poi2011]Lightning Conductor 决策单调性
[BZOJ2216][Poi2011]Lightning Conductor Description 已知一个长度为n的序列a1,a2,...,an.对于每个1<=i<=n,找到最小的非负 ...
随机推荐
- Node.js 使用爬虫批量下载网络图片到本地
图片网站往往广告众多,用Node.js写个爬虫下载图片,代码不长,省事不少,比手动一张张保存简直是天与地的区别.以前用Java也做过远程图片下载,但Node.js的下载速度更让人咂舌,这也是非阻塞式变 ...
- zStack学习笔记(原创,绝对不是抄的……)
我之前写的文章都没写上面那句,但是这篇写了,主要是因为zStack文章抄袭太严重……故此声明 因为涉及到数据的双向交互问题,所以在这里我考虑使用协议栈来实现数据的收发.首先说下如何在Zstack中添加 ...
- Cookie的增删改查
增加: 第一种方法:Response.Cookies[“UserName”].Value=”张三” Response.Cookies[“UserName”].Expires=DateTime.Now. ...
- 神技do{}while(false)
神技do{}while(false) do{}while(false)或者说do{}while(0),本人在linux源码中学得,起初看起来比较奇怪,但在处理连续流程中特别有用,例如ABC三个流程,A ...
- ST股福音:涨停潮开始! 最全ST摘帽股汇总!
本周ST股摘帽行情提前预演,ST股上演涨停潮,部分ST股甚至出现连续涨停.云财经在三季报披露之前曾经做过一期ST股摘帽分析,在三季报正式披 露完毕后,以及部分ST公司公布了2015年年报预告,ST股能 ...
- cassandra的primary key, partition key, cluster key,
https://stackoverflow.com/questions/24949676/difference-between-partition-key-composite-key-and-clus ...
- unity, 弹出panel一定要放在UI Hierarchy的底端
如上图,buyPanel放在最底端,为的是它弹出时屏蔽所有其它UI的消息.
- CentOS 加载/挂载光驱
1.mkdir /mnt/cdrom 2.mount /dev/cdrom /mnt/cdrom 3.挂载 umount /dev/cdrom /mnt/cdrom
- for zip
private void zip(ZipOutputStream out, File file, String base) { try { if (file.isDirectory()) { File ...
- QT错误笔记-Qt Creator needs a compiler set up to build. Configure a compiler in the kit options.
上午在linux环境下,使用QT编译一段C++代码,出现下列错误: 最近在stackoverflow上找到了答案: i was also having the same problem so what ...