[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;
}