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

  • 相关阅读:
    网页背景音乐
    CSS 实现背景半透明
    实战中总结出来的CSS常见问题及解决办法
    AsyncTask的简单使用
    ORMLiteDatabase的简单使用并且与其他的表相互联系
    传递消息--第三方开源--EventBus的简单使用
    通过messenger实现activity与service的相互通信
    通过Messenger与后台连接(单向操作,activity向service发送数据)
    怎么做QQ、微信等消息气泡
    通过bind实现activity与service的交互
  • 原文地址:https://www.cnblogs.com/neighthorn/p/6590063.html
Copyright © 2011-2022 走看看