题目描述

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.

一行,表示结果

样例输入

10
aabbaaabab

样例输出

3
Explanation of the example: The fragment that attains the difference of 3 in the number of occurrences of a and b is aaaba.
 
一道很好的思维题。
假设最后答案的那一段中最多的是a,最少的是b,那么答案就是(ra-la)-(rb-lb),其中l,r代表序列端点处的前缀和,变换之后也就是(ra-rb)+(lb-la),也就是求端点位置前缀和之差。我们设f[i][j]表示当前位置之前的某一位置i字符个数减j字符个数的最大值,g[i][j]表示当前位置i字符个数减j字符个数的值。因为每多一个位置只有一种字符的个数被改变,如果对答案有影响,那么当前位字符一定是答案中的那个最大值或者最小值。枚举其他25个字符,考虑当前位为最大值枚举字符为最小值或当前位为最小值枚举字符为最大值时对答案的影响来更新答案即可。时间复杂度O(26n)。
#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的更多相关文章

  1. 【BZOJ2213】[Poi2011]Difference DP

    [BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...

  2. BZOJ2213: [Poi2011]Difference

    2213: [Poi2011]Difference Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 343  Solved: 108[Submit][St ...

  3. BZOJ2213 [Poi2011]Difference 【乱搞】

    题目链接 BZOJ2213 题解 考虑任意一对点的贡献,单独拿出那些点所在位置 一个设为\(1\),一个设为\(-1\),从头到尾扫一遍维护前缀和,以及当前最小前缀和 两者相减更新答案 需要注意的是当 ...

  4. bzoj2213: [Poi2011]Difference(思维题)

       今天颓了一天T T 这题有两种写法... ①预处理出每种字符在原字符串中的位置,枚举两种字符作为最大值和最小值,把这两种字符的坐标归并排序,把最大值设为1,最小值设为-1,求最大子段和.注意因为 ...

  5. [bzoj2213][Poi2011]Difference_动态规划

    Difference bzoj-2213 Poi-2011 题目大意:已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最 ...

  6. bzoj 2213: [Poi2011]Difference

    Description A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We w ...

  7. POI2011题解

    POI2011题解 2214先咕一会... [BZOJ2212][POI2011]Tree Rotations 线段树合并模板题. #include<cstdio> #include< ...

  8. POI做题笔记

    POI2011 Conspiracy (2-SAT) Description \(n\leq 5000\) Solution 发现可拆点然后使用2-SAT做,由于特殊的关系,可以证明每次只能交换两个集 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. MySQL(一)MySQL基础介绍

    最近的学习内容是数据库相关的一些知识,主要以MySQL为主,参考书籍——<MySQL必知必会> MySQL学习及下载地址:https://dev.mysql.com/ MySQL学习使用注 ...

  2. C++11 并发指南二(std::thread 详解)

    上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...

  3. 9-(基础入门篇)云端安装MQTT服务器

    https://www.cnblogs.com/yangfengwu/p/9953703.html 记得把文件拷贝到上一节配置的和云端共享的那个盘里面,好拷贝文件到云服务器 进入到bin目录 咱先以控 ...

  4. java 之UDP编程

    大白话:每一台电脑都有自己的ip地址,向指定的ip地址发数据,数据就发送到了指定的电脑.UDP通信只是一种通信方式而已,其特点就不多说.有了ip地址数据就能发送到指定的电脑了,但是呢!我把数据发送到电 ...

  5. leetcode56:Merge Intervals

    大都是自定义了 Interval的比较方法. 突发奇想 int [] arr=new int[intervals.Count*2]; for(int i=0;i<intervals.Count; ...

  6. WPF Blend 脑洞大开的问题:如何用Blend得到或画出一个凹槽、曲面。

    原文:WPF Blend 脑洞大开的问题:如何用Blend得到或画出一个凹槽.曲面. 目标图: 步骤一(放置一个矩形,填充蓝色): 步骤二(复制该矩形,并调整边角,填充粉红色): 第三部:让图形部分重 ...

  7. Ionic 中badge的应用

    app中如果有服务端推送过来的消息,用户没有查看的话,出现一个数字提醒,类似微信的那种效果. 在Ionic中的实现过程还是很简单的: <ion-tab title="首页" ...

  8. SpringCloud Eureka参数配置项详解(转)

    Eureka涉及到的参数配置项数量众多,它的很多功能都是通过参数配置来实现的,了解这些参数的含义有助于我们更好的应用Eureka的各种功能,下面对Eureka的配置项做具体介绍,供大家参考. Eure ...

  9. 【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--请求处理结果适配篇(7/8)

    文章目录 前情概要 前面一大坨一大坨的代码把route.controller.action.attribute都搞完事儿了,最后剩下一部分功能就是串起来的调用. 那接下就说个说第二个中间件,也是最后一 ...

  10. C# 8中的Async Streams

    关键要点 异步编程技术提供了一种提高程序响应能力的方法. Async/Await模式在C# 5中首次亮相,但只能返回单个标量值. C# 8添加了异步流(Async Streams),允许异步方法返回多 ...