文章

37

粉丝

87

获赞

4

访问

12.6k

头像
括号匹配 题解:来了哦
P1501 西北工业大学2015机试题
发布于2024年3月7日 15:55
阅读数 256

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

int main()
{
    stack<char> stac;
    string s;
    cin >> s;
    int len = s.size();
    for (int i = 0; i < len; i++) {
        if (!stac.empty()) {
            char now = stac.top();
            if ((s[i] == ')' && now == '(') || (s[i] == ']' && now == '[')) {//匹配成功,出栈
                stac.pop();
            }
            else {
                stac.push(s[i]);
    &nbs...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发