Balancing Symbols

编程入门 行业动态 更新时间:2024-10-28 01:23:55

<a href=https://www.elefans.com/category/jswz/34/1715294.html style=Balancing Symbols"/>

Balancing Symbols

symbols匹配问题

#include<iostream>
#include<string>
using namespace std;struct Node
{char data;Node*next;
};struct LinkStack
{Node*top;
};LinkStack*create()
{LinkStack*stack = new LinkStack;stack->top = NULL;return stack;
}bool isEmpty(LinkStack*stack)
{return (stack->top == NULL);
}void pop(LinkStack*stack)
{Node*p = stack->top;stack->top = stack->top->next;delete p;
}void push(LinkStack*stack, char item)
{Node*p = new Node;p->data = item;p->next = stack->top;stack->top = p;
}char Top(LinkStack*stack)
{return stack->top->data;
}int main()
{int n;cin >> n;while (n-->0){string s;cin >> s;LinkStack*stack = create();bool flag = true;int n = s.size();for (int i = 0; i < n; i++){if (s[i] == '(' || s[i] == '[' || s[i] == '{')push(stack, s[i]);if (s[i] == ')'){if (!isEmpty(stack)){if (isEmpty(stack) && Top(stack) != '('){flag = false;break;}else{pop(stack);}}else{flag = false;break;}}else if (s[i] == ']'){if (!isEmpty(stack)){if (Top(stack) != '['){flag = false;break;}else{pop(stack);}}else{flag = false;break;}}else if (s[i] == '}'){if (!isEmpty(stack)){if (Top(stack) != '{'){flag = false;break;}else{pop(stack);}}else{flag = false;break;}}}if (!isEmpty(stack))flag = false;if (flag)cout << "Yes" << endl;if (!flag)cout << "No" << endl;delete stack;}return 0;
}

  

转载于:.html

更多推荐

Balancing Symbols

本文发布于:2024-03-06 12:10:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1715295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Balancing   Symbols

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!