STL中的栈 P1739 表达式括号匹配

STL中的栈

stack.top()
stack.empty()
stack.size()
stack.push()
stack.pop()

看个简单的例题(这种题我以前竟然不会做....)

#include<iostream>
#include<stack>
using namespace std;
stack<bool> brackets;
char s[257];

int main()
{
	cin>>s;
	bool flag=true;
	for(int i=0;s[i]!='@';i++)
	{
		if(s[i]=='(')
		{
			brackets.push(true);
		}
		else if(s[i]==')')
		{
			if(brackets.empty())
			{
				flag=false;
				break;
			}
			brackets.pop();      //先弹出的是那个和它匹配的( 
		}
	}
	if(!brackets.empty())
	{
		flag=false;
	}
	cout<<(flag?"YES":"NO");
	return 0; 
}

猜你喜欢

转载自www.cnblogs.com/serendipity-my/p/12663368.html