杭电1870题(水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1870
求B字符的深度,我第一次是直接求B前面有多少个(,但其实忽略了一种情况就是(()B),所以怎么办呢,遇到(深度加一,遇到)深度减一
代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int num;
	string s;
	while(getline(cin,s))
	{
		num=0;
		for(int i=0;i<s.length()&&s[i]!='B';i++)
		{
			if(s[i]=='(')  num++;
			else if(s[i]==')')  num--;
		}
		cout<<num<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39905917/article/details/87346727