文章

18

粉丝

0

获赞

87

访问

14.7k

头像
括号匹配问题 题解:
P1296 北京大学机试题
发布于 2026年3月
阅读数 430

#include<bits/stdc++.h>
using namespace std;

//思路:分别建立左括号和右括号栈,一个正向遍历,一个反向遍历
int main(){
    string str;
    while(cin>>str){
        cout<<str<<endl;
        vector<char> ans(str.size(),' ');//数组记录
        //左括号栈
        stack<char> a;
        for(int i=0;i<str.size();i++){
            if(str[i]=='('){
                a.push(str[i]);
                ans[i]=' ';
            }
            if(str[i]==')'){
                if(a.empty()) ans[i]='?';
                  else if(a.top()=='('){
      &nb...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发