E. Mishap in Club (CF 245E)
Posted clb123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
+-+-+
1
---
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\n",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; }
求++和--的最大差值
以上是关于E. Mishap in Club (CF 245E)的主要内容,如果未能解决你的问题,请参考以下文章