zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    	return 0;
    }
    

      


    By NeighThorn

  • 相关阅读:
    【监控】一些关于应用级别监控的总结
    【监控】WebServer入库与缓存更新代码优化小计
    【监控】天机镜——优土大数据平台应用级别监控利器
    【监控】数据平台运营实战之如何打造应用级别的监控系统
    【分布式协调器】Paxos的工程实现-Cocklebur状态转移
    【分布式协调器】Paxos的工程实现-cocklebur选举
    【分布式协调器】Paxos的工程实现-cocklebur简介(二)
    【分布式协调器】Paxos的工程实现-cocklebur简介(一)
    【语言基础】c++ 基本数据类型与字节数组(string,char [] )之间的转化方法
    【基础】利用thrift实现一个非阻塞带有回调机制的客户端
  • 原文地址:https://www.cnblogs.com/neighthorn/p/6590063.html
Copyright © 2011-2022 走看看