文章

54

粉丝

0

获赞

12

访问

1.2k

头像
括号的匹配 题解:
P1067 中山大学机试题
发布于2026年1月26日 19:43
阅读数 27

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

int n;
string str;

bool check()
{
	stack<char> st;
	for(int i=0;i<str.size();i++)
	{
		if(str[i]=='{')
		{
			if(st.empty())
			   st.push(str[i]);
			else
			   {
				   if(st.top()=='['||st.top()=='('||st.top()=='<')
					   return false;
				   else
					   st.push(str[i]);
			   }
		}
		else if(str[i]=='[')
        {
            if(st.empty())
			   st.push(str[i]);
			else
			   {
				   if(st.top()=='('||st.top()=='<')
					   return false;
				   else
					   st.push(str[i]);
			   }
        }
        else if(str[i]=='(')
        {
            if(st.empty())
			   st.push(str[i]);
			else
			   {
				   if(st.top()=='<')
					   return false;
				   else
					   st.push(str[i]);
			   }
        }
        else if(str[i]=='<')
        st.push(str[i]);
        else
        {
            if(st.empty())
                return false;
   ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发