#include <iostream>#include <string>#include <vector>using namespace std;string omitspace(string s);void erase(string& s);int _tmain(int argc, _TCHAR* argv[]){int num;cin >> num;char ch; cin.get(ch); vector<string> vec;for(int i = 0; i < num; i++) {char tmp[255];cin.getline(tmp,255,'/n');vec.push_back(string(tmp));}for(int i = 0;i < num; i++) {string s = omitspace(vec[i]);erase(s);erase(s);cout << s << endl;}return 0;}string omitspace(string s) {char ch[255];int chi = 0,si = 0;while (si < s.size()){if(s[si] != ' ') {ch[chi] = s[si];chi++;}si++;}ch[chi] = '/0';return string(ch);}void erase( string& s) {for(int i = s.size()-1; i >= 0; i--) {if (s[i] == ')'){int count = 0;int tmp = i;for (int j = i-1;j >= 0;j--){if(s[j] == ')') {count++;tmp = j;}if (s[j] == '(' && count == 0 ){if((j>0?s[j-1]!='-':j==0) || (tmp-j)==2) {s[j] = s[i] = ' ';break;}}else if(s[j] == '(' && count != 0)count--;}}}s = omitspace(s);} 收获:从后往前查找,利用count计数来判断是否为配对的左括号。