安神口中的水题

                                                                          Mishap in Club
 

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

 膜拜安神,这个题一开始想错了,以为是求最长的相同字串,。经过安神指点,原来这个题目就是一步一步的读,进来或者出去的就把相应的变量加减,一旦出去的人过多,说明总人数要增加,反之进去的人的情况也如此。

#include <iostream>
#include <cstdio>
using namespace std;

char rec[400];

int main()
{
    int num=1;
    int in=0,out=0;
    char ch;
    ch=getchar();
    if (ch=='+') in++;
    else out++;
    while ((ch=getchar())!='\n')
    {
        if (ch=='+')
        {
            in++;
            if (out) out--;
            else {num++;}
        }
        else
        {
            out++;
            if (in) in--;
            else {num++;}
        }
    }
    cout<<num<<endl;
    return 0;
}

  

 

原文地址:https://www.cnblogs.com/kkrisen/p/2875533.html