栈 - 愚人节的礼物
题目
四月一日快到了,Vayko想了个愚人的好办法——送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放礼物的盒子里不再放其他盒子。
用()表示一个盒子,B表示礼物,Vayko想让你帮她算出愚人指数,即最少需要拆多少个盒子才能拿到礼物。
输入
本题目包含多组测试,请处理到文件结束。
每组测试包含一个长度不大于1000,只包含’(‘,’)'和’B’三种字符的字符串,代表Vayko设计的礼物透视图。
你可以假设,每个透视图画的都是合法的。
#输出
对于每组测试,请在一行里面输出愚人指数。
#黑窗输入
((((B)()))())
(B)
##黑窗结果
4
1
程序
#include "stdafx.h"
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
stack<char> sta;
char s[10001];
while(scanf("%s",&s)!=EOF){
int index=0;
while(s[index]){
if(s[index]=='('){
sta.push('(');
}
else if(s[index]==')'){
sta.pop();
}
else if(s[index]=='B'){
printf("%d\n",sta.size());
break;
}
index++;
}
}
return 0;
}
/*另一种*/
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
stack<int> sta;
int main()
{
char a;
while(scanf("%c",&a)!=EOF)
{
int num=0;
if(a=='(')
{
num++;
sta.push(num);
}
else if(a==')')
{
sta.pop();
}
else if(a=='B')
{
printf("%d\n",sta.size());
}
}
return 0;
}