【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. [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket错误解决方法总结

    今天做一个特殊的业务处理,用JDBC连接SQLServer数据库载入驱动的时候,报例如以下错误: java.sql.SQLException: [Microsoft][SQLServer 2000 D ...

  2. &lt;十&gt;读&lt;&lt;大话设计模式&gt;&gt;之观察者模式

    观察者模式也是比較简单的一种模式,可能从名字上理解无法明确,但真正理解其含义之后就非常easy了,说实话在自己来发的项目中自己也用到过.仅仅只是不知道它叫观察者罢了,仅仅要懂面向对象的对继承多态理解非 ...

  3. WifiStateMachine学习笔记

    WifiStateMachine 1. 初始化 传入接口名称wlanInterface 新建一个WiFi类型的NetworkInfo 发一个ssid为null的广播 电池 NetworkManagem ...

  4. Fiddler-常用技巧

    1.详情面板 1).Inspectors 标签栏进行请求和响应结果分析 2).AutoResponder 对匹配 URL 进行自动返回, 可以使用字符.URL.正则表达式 3).Composer 模拟 ...

  5. Python 的错误和异常处理

    语法错误 Python 的语法错误或者称之为解析错,如下: >>> while True print('Hello world') File "<stdin>& ...

  6. 《Mining the Web:Transforming Customer Data into Customer Value》读后札记

    <Mining the Web:Transforming Customer Data into Customer Value> <Web数据挖掘:将客户数据转化为客户价值> — ...

  7. 9、Linux驱动的杂项设备

    杂项设备,是字符设备中的特殊,它的主设备号,是 10,不同的杂项设备,通过次设备号进行区分. 1.注册与注销 int misc_register(struct miscdevice * misc) 完 ...

  8. 当执行sql更新失误,添加了错误的判断条件,导致数据表数据出错时,如何利用备份数据库实现联合更新还原数据。

    首先得有备份数据库,没有备份肯定无法联合更新回复. 我错误更新了 [SBSDB].[dbo].[wallet_user_info]中的用户名user_name 我的备份数据库及对应数据表SBSDBTe ...

  9. 转FTP协议详解

    转自:http://www.cnblogs.com/li0803/archive/2010/11/16/1878833.html FTP 是File Transfer Protocol(文件传输协议) ...

  10. linux ipvsadm安装

    cd /usr/src/ wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24-6.src.rpmrpm -i ...