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;
    }
    

      

    求++和--的最大差值

  • 相关阅读:
    CSS布局之浮动原理
    img中的alt和title的区别
    重绘+重排+项目优化
    事件冒泡、事件捕获和事件委托
    js数组方法总结
    flex布局
    冒泡排序
    函数节流和函数防抖
    从一个url地址最终到页面渲染完成,发生了什么
    如何让网站变灰
  • 原文地址:https://www.cnblogs.com/clb123/p/10807609.html
Copyright © 2011-2022 走看看