【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

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.

题解:我承认我做的可能有点麻烦~

先枚举出现次数最少的字符,然后设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的更多相关文章

  1. 【BZOJ2525】[Poi2011]Dynamite 二分+树形DP

    [BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...

  2. 【BZOJ2525】[Poi2011]Dynamite(二分,树形dp)

    [BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...

  3. 【BZOJ2217】[Poi2011]Lollipop 乱搞

    [BZOJ2217][Poi2011]Lollipop Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T& ...

  4. 【LG3527】[POI2011]MET-Meteors

    [LG3527][POI2011]MET-Meteors 题面 洛谷 题解 整体二分. 每次二分\(mid\),如果到时间\(mid\)以收集过\(P_i\)就存入子序列\(L\),否则存入子序列\( ...

  5. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  6. 【BZOJ2527】[Poi2011]Meteors 整体二分

    [BZOJ2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...

  7. 【BZOJ2530】[Poi2011]Party (xia)构造

    [BZOJ2530][Poi2011]Party Description 给定一张N(保证N是3的倍数)个节点M条边的图,并且保证该图存在一个大小至少为2N/3的团. 请输出该图的任意一个大小为N/3 ...

  8. 【题解】POJ1934 Trip (DP+记录方案)

    [题解]POJ1934 Trip (DP+记录方案) 题意: 传送门 刚开始我是这么设状态的(谁叫我DP没学好) \(dp(i,j)\)表示钦定选择\(i\)和\(j\)的LCS,然而你会发现这样钦定 ...

  9. 【BZOJ2216】[Poi2011]Lightning Conductor 决策单调性

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

随机推荐

  1. 持续集成之代码质量管理-Sonar

    原文:http://blog.csdn.net/abcdocker/article/details/53840582 Sonar介绍 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Son ...

  2. Java8:纠结的默认方法

    [编程导论(Java)·4.3Java接口] 在[0.3.1 Java简单介绍]中,有这么一段话:"请注意:Java并不是作为教学语言设计的.世界各地的大学在讲授Java的过程中均遇到一些教 ...

  3. valgrind的callgrind工具进行多线程性能分析

    1.http://valgrind.org/downloads/old.html 2.yum install valgrind Valgrind的主要作者Julian Seward刚获得了今年的Goo ...

  4. 【Python 数据分析】pandas模块

    上一节,我们已经安装了numpy,基于numpy,我们继续来看下pandas pandas用于做数据分析与数据挖掘 pandas安装 使用命令 pip install pandas 出现上图表示安装成 ...

  5. Oracle转化成为百分比

    两种方式都行: ),)||'%' 百分比 from dual; ),'99D99')||'%' 百分比 from dual 第一种方式通过round可以自己选择精确到位数.

  6. 使用history.pushState()和popstate事件实现AJAX的前进、后退功能

    上一篇文章中.我们使用location.hash来模拟ajax的前进后退功能.使用location.hash存在以下几个问题: 1.使用location.hash会导致地址栏的url发生变化.用户体验 ...

  7. WebLogic92数据源配置

    一. 将数据库连接所需的包导入(非常重要) 最简单的方法就是,将所需jar包复制至%MYDOMAIN_HOME%/lib中,约定本应用域的名称为“ MyDomain”,根路径为%MYDOMAIN_HO ...

  8. 6、udev机制

        udev 机制,主要实现的是当设备连接系统的时候,在 /dev 目录下,自动创建设备节点.   1.1.工作方式     当设备连接或者移除的时候,内核会发出热拔插事件(hotplug eve ...

  9. Mysql数据库分库备份,分表备份

    分库备份 #!/bin/sh DBPATH=/server/backup MYUSER=root MYPASS=oldboy123 SOCKET=/data/3306/mysql.sock MYCMD ...

  10. Linux 进程创建二(execve和wait)

    三:execve系统调用 int execve(const char *filename, char *const argv[],char *const envp[]); fork创建了一个新的进程, ...