Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
难道贪心法不对 ?#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; bool* match = new bool[s.length()]; int i, j; for(i = 0; i < s.length(); ++i) match[i] = false; int sleft, sright, bleft, bright; sleft = sright = bleft = bright = 0; string result; for(i = 0; i < s.length(); ++i) { if(s[i] == '(') { sleft = 1; sright = 0; for(j = i+1; j < s.length(); ++j) { if(s[j] == '(') ++sleft; else if(s[j] == ')') ++sright; if(sleft == sright) break; } if(sleft == sright) { result += '('; match[j] = true; } else result += "()"; } else if(s[i] == ')') { if(match[i]) result += ')'; else result += "()"; } else { result += s[i]; } } s = result; result = ""; delete[] match; match = new bool[s.length()]; for(i = 0; i < s.length(); ++i) match[i] = false; for(i = 0; i < s.length(); ++i) { if(s[i] == '[') { sleft = 0; sright = 0; bleft = 1; bright = 0; for(j = i+1; j < s.length(); ++j) { if(s[j] == '[') { if(sleft != sright) break; ++bleft; } else if(s[j] == ']') { if(sleft != sright) break; ++bright; } else if(s[j] == '(') ++sleft; else if(s[j] == ')') ++sright; if(bleft == bright) break; } if(bleft == bright) { result += '['; match[j] = true; } else result += "[]"; } else if(s[i] == ']') { if(match[i]) result += ']'; else result += "[]"; } else { result += s[i]; } } delete[] match; cout << result << endl; return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator