A. A pile of stones

题目链接
Vasya has a pile, that consists of some number of stones. n times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile.

You are given n operations which Vasya has made. Find the minimal possible number of stones that can be in the pile after making these operations.

Input
The first line contains one positive integer n — the number of operations, that have been made by Vasya (1≤n≤100).

The next line contains the string s, consisting of n symbols, equal to “-” (without quotes) or “+” (without quotes). If Vasya took the stone on i-th operation, si is equal to “-” (without quotes), if added, si is equal to “+” (without quotes).

Output
Print one integer — the minimal possible number of stones that can be in the pile after these n operations.

input

3
---

output

0

input

4
++++

output

4

input

2
-+

output

1

input

5
++-++

output

3

Note
In the first test, if Vasya had 3 stones in the pile at the beginning, after making operations the number of stones will be equal to 0. It is impossible to have less number of piles, so the answer is 0. Please notice, that the number of stones at the beginning can’t be less, than 3, because in this case, Vasya won’t be able to take a stone on some operation (the pile will be empty).

In the second test, if Vasya had 0 stones in the pile at the beginning, after making operations the number of stones will be equal to 4. It is impossible to have less number of piles because after making 4 operations the number of stones in the pile increases on 4 stones. So, the answer is 4.

In the third test, if Vasya had 1 stone in the pile at the beginning, after making operations the number of stones will be equal to 1. It can be proved, that it is impossible to have less number of stones after making the operations.

In the fourth test, if Vasya had 0 stones in the pile at the beginning, after making operations the number of stones will be equal to 3.

题意:给你一串只有’+’,’-'组成的字符串,‘+’代表给石子堆加一,‘-’代表给石子堆减一,初始石子堆为0,在每一次操作之前,保证石子堆非空,问你将字符串中的命令执行完之后石子堆有多少个
题解:模拟,注意如果石子堆为0,那么将不会执行‘-’操作

#include<bits/stdc++.h>
using namespace std;
int main(){
	char c;
	int n,ans = 0;
	scanf("%d",&n);
	for(int i = 0;i < n;i++){
		scanf(" %c",&c);
		if(c == '-' && ans - 1 >= 0) ans--;
		else if(c == '+') ans++;
	}
	printf("%d\n",ans);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值