10 solutions

  • 1
    @ 2022-4-2 14:29:29
    #include <iostream>
    #include <map>
    #include <stack>
    #include <cmath>
    #include <string>
    
    using namespace std;
    
    map<char, int> m;
    
    
    int main05(void) {
    	m['<'] = 1;	m['>'] = -1;
    	m['('] = 2;	m[')'] = -2;
    	m['['] = 3;	m[']'] = -3;
    	m['{'] = 4;	m['}'] = -4;
    
    	int n;
    	cin >> n;
    
    	for (int i = 0; i < n; ++i) {
    		stack<char> s;
    		string str;
    		cin >> str;
    		bool flag = true;// 默认是 YES 
    		for (int j = 0; j < str.size(); j++) {
    			if (s.empty()) {
    				s.push(str[j]);
    			}
    			else {
    				int ch = s.top();
    				if (m[ch] < m[str[j]]) {
    					flag = false;
    					break;
    				}
    				else if(m[ch] + m[str[j]] == 0) {
    					s.pop();
    				}
    				else {
    					s.push(str[j]);
    				}
    			}
    		}
    		if (flag && s.empty()) {
    			cout << "YES" << endl;
    		}
    		else if (flag && !s.empty()) {
    			cout << "NO" << endl;
    		}
    		else if (flag == false) {
    			cout << "NO" << endl;
    		}
    		
    	}
    
    	return 0;
    }
    

    Information

    ID
    296
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    7
    Tags
    # Submissions
    176
    Accepted
    36
    Uploaded By