文章
2
粉丝
305
获赞
2
访问
17.2k
#include<bits/stdc++.h>
using namespace std;
int val[127];
int n;
int main()
{
val['<'] = 1; val['('] = 2; val['['] = 3; val['{'] = 4;//定义括号优先级
cin >> n;
while (n--)
{
stack <char> st;//定义栈
string s;
cin >> s;
int flag = 0;
for (int i = 0; i < s.length(); i++){
if(!st.empty()){//如果栈不为空
if((s[i]=='<'||s[i]=='('||s[i]=='['||s[i]=='{')&&(val[st.top()]<val[s[i]])) //如果括号优先级出错
flag = 1;
else if(st.top()=='<'&&s[i]=='>'||st.top()=='('&&s[i]==')'||st.top()=='['&&s[i]==']'||st.top()=='{'&&s[i]=='}') //如果括号匹配,出栈
st.pop();
else st.push(s[i]);//否则入栈
}
else st.push(s[i]);//栈为空时,入栈
}
if(flag == 1) cout<<"NO"<<endl;
else if(st.empty())
cout<<"YES"<<endl...
登录后发布评论
暂无评论,来抢沙发