关于c++:括号匹配算法基础上机试题

4次阅读

共计 818 个字符,预计需要花费 3 分钟才能阅读完成。

给定一个只包含 ‘(‘,’)’,'{‘,’}’,'[‘,’]’ 的字符串,判断字符串是否无效。
无效字符串需满足:

1. 左括号必须用雷同类型的右括号闭合。2. 左括号必须以正确的程序闭合。3. 留神空字符串可被认为是无效字符串。
//#include <algorithm>
//#include <cmath>
//#include <iostream>
//#include <map>
//#include <queue>
//#include <stack>
//#include <string>
//#include <string.h>
//#include <vector>
//using namespace std;
//bool Ispair(char a, char b);
// 
//int main(){
//    string s = "";
//    while(getline(cin, s)){//        if(s.empty()){
//            cout << "true" <<endl;
//            continue;
//        }else{
//            stack<char> st;
//            st.push(s[0]);
//            for(int i = 1; i < (int)s.size(); i++){//                if(!st.empty() && Ispair(st.top(),s[i])){//                    st.pop();
//                }else{//                    st.push(s[i]);
//                }
//            }
//            if(st.empty()){
//                cout << "true" << endl;
//            }else{
//                cout << "false" << endl;
//            }
//        }
//    }
//    return 0; 
//}
//
//bool Ispair(char a, char b){//    if(a == '(' && b == ')'){
//        return true;
//    }
//    if(a == '[' && b == ']'){
//        return true;
//    }
//    if(a == '{' && b == '}'){
//        return true;
//    }
//    return false;
//}

正文完
 0