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

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.

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 的代码没看懂,唉,挖个坑,以后来填

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 500+100
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define mod 1000000007
23 using namespace std;
24 inline int read()
25 {
26     int x=0,f=1;char ch=getchar();
27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
28     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
29     return x*f;
30 }
31 int n,ans=0,f[30][30],g[30][30];
32 int main()
33 {
34     freopen("input.txt","r",stdin);
35     freopen("output.txt","w",stdout);
36     n=read();
37     for1(i,n)
38      {
39          char ch=getchar();
40          while(ch>‘z‘||ch<‘a‘)ch=getchar();
41          int x=ch-‘a‘;
42          for0(j,25)
43           {
44              f[x][j]++;
45              f[j][x]--;
46              if(!g[j][x])g[j][x]=1;
47              if(g[j][x]==2){g[j][x]=1;f[j][x]++;}
48              if(f[j][x]<=-1){f[j][x]=-1;g[j][x]=2;}
49              if(g[j][x])ans=max(ans,f[j][x]);
50              if(g[x][j])ans=max(ans,f[x][j]);
51           }
52      }
53     printf("%d\n",ans);
54     return 0;
55 }

时间: 2024-12-17 15:20:57

BZOJ2213: [Poi2011]Difference的相关文章

【bzoj2213】[Poi2011]Difference dp

题目描述 已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最大.求这个个数. 输入 第一行,n第二行,该字符串1<=n<=1000000 输出 一行,表示结果 样例输入 10 aabbaaabab 样例输出 3 题解 dp 令$sum[i][j]$表示前$i$个字母中$j$的出现次数,那么题目所求的是$Max((sum[r][i]-sum[l-1][i])-(sum[r][j]-sum[l-1][j]))=Max(

python: the difference between append and extend

Data Analysis: indoor localization using received signal strength (RSS) An error about list operation in python: append and extend elements We define a list A to storage objects, which the length is unknown, and list B and C to storage the final resu

389. Find the Difference

Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t. Example: Input: s = "abcd" t = "ab

The Difference Between a Router, Switch and Hub

Some technicians have a tendency to use the terms router, switch and hub interchangeably,  but have you ever wondered what the difference is? Some technicians have a tendency to use the terms router, hub and switch interchangeably. One minute they're

Leetcode 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

The difference of Methods and Functions

Functions Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed. Swift’s unified function syntax

lintcode 中等题:maximum subarray difference 最大子数组差

题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1],返回 6 注意 子数组最少包含一个数 挑战 时间复杂度为O(n),空间复杂度为O(n) 解题 刚做了数组中两个子数组和的最大值,这一题是求差,感觉上题的求解思想应该是可以用的 A B 分别是两个子数组的和,则: 所以 当A>B 的时候A越大越好 B越小越好 当A<B 的时候B越大越好 A越小越好

[转载]Difference between &lt;context:annotation-config&gt; vs &lt;context:component-scan&gt;

在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:component-scan>到底有什么不同? 原文出处:http://stackoverflow.com/a/7456501 <context:annotation-config> is used to activate annotations in beans already registered in

Set Difference(所有子集的最值差)

点击打开题目链接https://www.codechef.com/problems/SETDIFF Set Difference Problem code: SETDIFF SUBMIT ALL SUBMISSIONS All submissions for this problem are available. Churu is working as a data scientist in Coderpur. He works on a lot of data on the daily bas