题目:https://www.nowcoder.com/practice/3bad4a646b5b47b9b85e3dcb9488a8c3?tpId=40&tqId=30993&tPage=2&rp=2&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking
栈的简单应用,但也写了二十多分钟
代码
#include <cstdio>#include <stack>#include <string>#include <iostream>#include <algorithm>using namespace std;string table = "(){}[]";stack<char> s;int main(){ int n; while(scanf("%d", &n) != EOF){ getchar(); for(int i = 0; i < n; i++){ string input; getline(cin, input); while(!s.empty()) s.pop(); // 清空栈 s.push('#'); for(int j = 0; j < input.size(); j++){ if(table.find(input[j]) != string::npos){ int pos = table.find(input[j]); if(pos % 2 == 0){ s.push(input[j]); continue; } if(s.top() == table[pos - 1]){ s.pop(); } else s.push(input[j]); } } if(s.size() == 1) printf("yes\n"); else printf("no\n"); } }}