文章
40
粉丝
607
获赞
68
访问
419.7k
#include
#include
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0;i < n;i++) {
string s;
stack st;//每次循环重新开辟新stack不用清空
cin>>s;
for (int j = 0;j < s.size();j++) {//开始逐个字符匹配
char now = s[j];
if (st.empty()) {//如果栈空直接psuh
st.push(now);
}
else {//不空看栈top元素和now元素是否匹配,匹配pop掉
if (st.top() == '<') {
if (now == '>') {
st.pop();
}
else {//不匹配,查看now元素是否符合括号的顺序,符合push,不符合break掉
if (now == '<') {
st.push(now);
}
else {
break;
}
}
}
else if (st.top() == '(') {
if (now == ')') {
st.pop();
}
else {
if (now == '(' || now == '<') {
st.push(now);
}
else {
break;
}
}
}
else if (st.top() == '[') {
if (now == ']') {
st.pop();
}
...
登录后发布评论
暂无评论,来抢沙发