zoukankan      html  css  js  c++  java
  • 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
    ",ans);         
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    苹果新的编程语言 Swift 语言进阶(五)--控制流
    苹果新的编程语言 Swift 语言进阶(四)--字符串和收集类型
    苹果新的编程语言 Swift 语言进阶(三)--基本运算和扩展运算
    苹果新的编程语言 Swift 语言进阶(二)--基本类型
    第一篇 android架构是如何满足设计目标的?
    第三篇 android 应用开发模式之MVC模式及Observer模式
    第二篇 android应用开发模式之模板模式
    为什么带网格(mesh)的模型添加了刚体Rigidbody和MeshCollider,还是会从地板穿过去?
    Mecanim动画模型规范
    HTC Vive 体验的折腾经历
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4003891.html
Copyright © 2011-2022 走看看