zoukankan      html  css  js  c++  java
  • E. Mishap in Club (CF 245E)

    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

    Examples

    Input
    +-+-+
    Output
    1
    Input
    ---
    Output
    3


    思路:‘+’表示有一个人进咖啡厅,‘-’表示有一个人离开咖啡厅。同一个人可以可以多次进出咖啡厅,问咖啡厅最少流动了多少不同的人。
    第一种思路:
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e3+10;
    char str[maxn];
    int main()
    {
        int i,j;
        scanf("%s",str));
            int in=0,out=0;  //in表示咖啡厅里的人数,out表示咖啡厅外面的人数
            int len=strlen(str);
            for(i = 0; i < len; i++)
            {
                if (str[i]=='+')  //进来一个人
                {
                    in++;   //咖啡厅里面的人数自增
                    if (out)    //如果外面有人则就当作是外面的一个人进来了
                        out--;
                }
                else if (str[i]=='-')  //出去一个人
                {
                    out++;      //咖啡厅外面的人数自减
                    if (in)     //如果里面有人就当作是里面的一个人出去了
                        in--;
              }
          }
         printf("%d
    ",in+out);
        return 0;
    }
    

      第二种思路:实际为

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn=1e3;
    int main(){
        char ch[maxn];
        cin >> ch;
        int sum1 = 0,sum2 = 0,len=strlen(ch),max;
        for(int i=0; i < len; i++){
            if(ch[i] == '+')
                sum1++;
            else if(ch[i] == '-')
                sum2++;
            if(sum1>sum2&&max<sum1-sum2)
                 max=sum1-sum2;
            else if(sum2>sum1&&max<sum2-sum1)
                 max=sum2-sum1;
        }
        cout <<max<< endl;
        return 0;
    }
    

      

    求++和--的最大差值

  • 相关阅读:
    获取一个表的,字段,类型,长度,是否主键,是否为空,注释 等信息
    单个页面Request编码方式的改变,无需改动Web.config~
    关于锚点页内链接跳转出现问题(不响应,没有反应)的解决方法(ZT)
    40种网站设计常用技巧~
    在MasterPage中检验session是否存在~
    如何避免重构带来的危险
    早该知道的7个JavaScript技巧
    30个提高Web程序执行效率的好经验
    我学编程时犯的最大两个错误
    C# 中get和set属性的作用
  • 原文地址:https://www.cnblogs.com/clb123/p/10807609.html
Copyright © 2011-2022 走看看