一本通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 @ 2022-02-13 16:48  lwx_R  阅读(280)  评论(0编辑  收藏  举报