Advertisement

Vjudge 基础数据结构中的栈(1)题解

阅读量:

原题地址:https://vjudge.net/problem/HRBUST-1549#author=0
栈的经典应用题目:括号匹配问题

复制代码
    #include<iostream>
    #include<stack>
    #include<cstring>
    
    using namespace std;
    
    stack<char>a, st;
    
    char s[105];
    
    int main()
    {
    	cin.tie(0);
    	cout.tie(0);
    	ios::sync_with_stdio(false);
    	while(gets(s)){
    		st = a;
    		int r1 = 0, r2 = 0;
    		for(int i = 0; i < strlen(s); i ++ ){
    			if(s[i] == ')' || s[i] == ']' || s[i] == '}'){
    				r2 ++ ;
    			}
    			if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
    				st.push(s[i]);
    				r1 ++ ;
    			}

全部评论 (0)

还没有任何评论哟~