一本通1203 括号匹配

复制代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <iterator>
#include <vector>
#define maxn 10000005
typedef long long ll;
using namespace std;
/*
    1.遇到左括号入栈
    
    2.遇到右括号,看栈是否为空:
    空栈则便是该右括号不匹配置为?;
    栈不空则出栈左括号表示匹配
    
    3.最后看栈是否为空,
    栈空则便是所有左括号匹配完成,
    否则出栈左括号置为$
*/
 
char a[maxn];
int main(){
    string s;
    stack<int> st;
    while(cin>>s){
        
        int l=s.length();
        memset(a,' ',100);
        for(int i=0;i<l;i++){
            if(s[i]=='('){
                st.push(i);
                
            }else if(s[i]==')'){
                if(!st.empty()){
                    st.pop();
                }else{
                
                    a[i]='?';
                }
            }
        }
        
        while(!st.empty()){
            a[st.top()]='$';
            st.pop();
        
        }
        cout<<s<<endl;
        cout<<a<<endl;
        
    }    


    return 0;
}
复制代码

 

posted @   lwx_R  阅读(315)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示