BZOJ 2213: [Poi2011]Difference

2213: [Poi2011]Difference

Time Limit: 10 Sec  Memory Limit: 32 MB
Submit: 481  Solved: 171
[Submit][Status][Discuss]

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

 分析:

考虑$f[i][j]$代表当前字符$i$减去字符$j$的个数...

那么答案位$max{ f[r][i][j]-f[l-1][i][j] }$...

当右端点从$x$变为$x+1$的时候最多改变52个值,所以暴力更新,同时维护最小的$f[i][j]$和次小的$f[i][j]$,最小值和次小值的$cnt[j]$不同,因为要求$j$的个数不能为$0$...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;

const int maxn=1000000+5,maxm=26+5;

int n,ans,tot[maxm],cnt[maxm][maxm],Min[2][maxm][maxm],val[2][maxm][maxm];
char str[maxn];

inline void update(int x,int y){
	if(val[0][x][y]!=tot[y])
		ans=max(cnt[x][y]-Min[0][x][y],ans);
	else if(val[1][x][y]!=tot[y])
		ans=max(cnt[x][y]-Min[1][x][y],ans);
	if(cnt[x][y]<Min[0][x][y]&&val[0][x][y]!=tot[y]){
		Min[1][x][y]=Min[0][x][y];
		val[1][x][y]=val[0][x][y];
		Min[0][x][y]=cnt[x][y];
		val[0][x][y]=tot[y];
	}
	else if(cnt[x][y]<Min[0][x][y]){
		Min[0][x][y]=cnt[x][y];
		val[0][x][y]=tot[y];
	}
	else if(cnt[x][y]<Min[1][x][y]&&val[0][x][y]!=tot[y]){
		Min[1][x][y]=cnt[x][y];
		val[1][x][y]=tot[y];
	}
}

signed main(void){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	scanf("%d%s",&n,str+1);
	for(int i=1,k,t;i<=n;i++)
		for(k=0,t=str[i]-'a',tot[t]++;k<26;k++)
			if(k!=t)
				cnt[t][k]++,update(t,k),
				cnt[k][t]--,update(k,t);
	printf("%d\n",ans);
	return 0;
}

  


By NeighThorn

转载于:https://www.cnblogs.com/neighthorn/p/6590063.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值