• cf 245 E. Mishap in Club


    E. Mishap in Club
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Polycarpus just has been out of luck lately! As soon as he found a job in the "Binary Cat" cafe, the club got burgled. All ice-cream was stolen.

    On the burglary night Polycarpus kept a careful record of all club visitors. Each time a visitor entered the club, Polycarpus put down character "+" in his notes. Similarly, each time a visitor left the club, Polycarpus put character "-" in his notes. We know that all cases of going in and out happened consecutively, that is, no two events happened at the same time. Polycarpus doesn't remember whether there was somebody in the club at the moment when his shift begun and at the moment when it ended.

    Right now the police wonders what minimum number of distinct people Polycarpus could have seen. Assume that he sees anybody coming in or out of the club. Each person could have come in or out an arbitrary number of times.

    Input

    The only line of the input contains a sequence of characters "+" and "-", the characters are written one after another without any separators. The characters are written in the order, in which the corresponding events occurred. The given sequence has length from 1 to 300 characters, inclusive.

    Output

    Print the sought minimum number of people

    Sample test(s)
    Input
    +-+-+
    Output
    1
    Input
    ---
    Output
    3

    其实求++和--的最大差值了。囧。。。
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5     int max,len;
     6     int sum1,sum2;
     7     char str[310];
     8     while(~scanf("%s",str))
     9     {
    10         max=0;
    11         len=strlen(str);
    12         for(int i=0;i<len;i++)
    13         {
    14             sum1=0;sum2=0;
    15             for(int j=i;j<len;j++)
    16             {
    17                 if(str[j]=='+')
    18             {
    19                 sum1++;
    20             }
    21             else 
    22             {
    23                 sum2++;
    24             }
    25             if(sum1>sum2&&max<sum1-sum2)
    26             max=sum1-sum2;
    27             else if(sum2>sum1&&max<sum2-sum1)
    28             max=sum2-sum1;
    29             }
    30              
    31         }
    32         printf("%d\n",max);
    33     }
    34 }
  • 相关阅读:
    20 类中的函数重载
    19 友元的尴尬能力
    18 类的静态成员函数
    17 类的静态成员变量
    16 经典问题解析二
    15 临时对象
    Lucene4.6查询时完全跳过打分,提高查询效率的实现方式
    Lucene4.6 把时间信息写入倒排索引的Offset偏移量中,并实现按时间位置查询
    Lucene6去掉了Filter但是可以用BooleanQuery实现Filter查询
    Dom4j解析语音数据XML文档(注意ArrayList多次添加对象,会导致覆盖之前的对象)
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2780002.html
Copyright © 2020-2023  润新知