zoukankan      html  css  js  c++  java
  • CF w1d1 A. A pile of stones

    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.

    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.

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    	int n,mark,ans=0;
    	string s;
    	cin>>n;
    	cin>>s;
    	if(s[0]=='+'){
    		for(int i=0;i<s.size();i++){
    			if(s[i]=='+')ans++;
    			if(s[i]=='-'&&ans>=1)ans--;
    		}
    	}
    	else
    	{
    		for(int i=0;i<s.size();i++)if(s[i]!='-'){
    			mark=i;
    			break;
    		}
    		for(int i=mark;i<s.size();i++){
    			if(s[i]=='+')ans++;
    			if(s[i]=='-'&&ans>=1)ans--;
    		}
    	}
    	cout<<ans;
    	return 0;
    } 
    
  • 相关阅读:
    基于term vector深入探查数据
    oracle 行转列
    oracle 统计成绩
    最全最新个税计算公式---今天你税了吗?
    .net反混淆脱壳工具de4dot的使用
    使用ILSpy软件反编译.Net应用程序的方法及注意事项
    EmguCV使用Stitcher类来拼接图像
    从睡姿就可以看出你的性格,据说非常准,快存!
    分享几个.NET WinForm开源组件,纪念逐渐远去的WinForm。。。
    【转载】关于.NET下开源及商业图像处理(PSD)组件
  • 原文地址:https://www.cnblogs.com/LiangYC1021/p/12656062.html
Copyright © 2011-2022 走看看