实训三#3.7愚人节礼物

[problem description]
April 1 is coming. Vayko thought of a good way for fools - giving gifts. Hey, don't think too well. This gift is not so simple. Vayko prepared a pile of boxes for fools, only one of which contained gifts. You can put zero or more boxes in the box. Suppose there are no other boxes in the gift box. Use () to represent a box and B to represent a gift. Vayko wants you to help her calculate the fool's index, that is, how many boxes need to be opened at least to get the gift.
[input form]
This topic contains multiple groups of tests. Please deal with it until the end of the document. Each group of tests contains a string with a length of no more than 1000 and only three characters' (',' ') and' B ', representing the gift perspective designed by vayko. You can assume that every perspective picture is legal.
 

【样例输入】

((((B)()))())
(B)

【样例输出】

4
1

#include <bits/stdc++.h>
using namespace std;

int main()
{
	string l; int p;
	while (cin >> l)
	{
		p = 0;
		while (l[p+1] != 0)
		{
			if (l[p] == '(' && l[p+1] == ')')
			{
				l.erase(p, 2);
				p--;
			}
			else p++;
		}
		p = 0;
		while (l[p] != 'B') p++;
		cout << p << endl;
	}
	return 0;
}

这道题的思路是,把没有用的括号全部去掉,然后只留下有用括号,然后一层一层地剥开礼物盒,计算层数即可~

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

int main(){
    string str = "hello c++! +++";
    // 从位置pos=10处开始删除,直到结尾
    // 即: " +++"
    str.erase(10);
    cout << '-' << str << '-' << endl;
    // 从位置pos=6处开始,删除4个字符
    // 即: "c++!"
    str.erase(6, 4);
    cout << '-' << str << '-' << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值