括号匹配
作者:
阿飞大魔王
,
2024-09-10 20:10:23
,
所有人可见
,
阅读 5
#include<bits/stdc++.h>
using namespace std;
bool bracketMatch(string s)
{
stack<char>st;
for(char x : s)
{
if(x=='('||x=='['||x=='{')
st.push(x);
else
{
if(st.empty())return false;
char a=st.top();
st.pop();
if((x==')'&&a!='(')||
(x==']'&&a!='[')||
(x=='}'&&a!='{'))
return false;
}
}
return st.empty();
}
int main()
{
string s1 = "((()))";
string s2 = "([{{}])";
if(bracketMatch(s1))
cout << "s1 matched" << endl;
else
cout << "s1 not matched" << endl;
if(bracketMatch(s2))
cout << "s2 matched" << endl;
else
cout << "s2 not matched" << endl;
return 0;
}