fork download
  1. //http://i...content-available-to-author-only...e.com/AbO4tw
  2. #include <iostream>
  3. #include <vector>
  4. #include <map>
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10. vector<string> bracesExpressionExamples = {
  11. "({[{}]{}[]})",
  12. "({}}{[{}]{}[]})",
  13. "({[{}]{}[]}",
  14. "({[{}]{}]})",
  15. "({[{}{}[]})",
  16. "",
  17. "{}"
  18. };
  19.  
  20. string openBrace = "({[";
  21. string closeBrace = ")}]";
  22.  
  23. typedef map<char, char> otc;
  24. const otc& openToCloseBrace(){
  25. static const otc o2c([](){
  26. otc o2c;
  27. transform(
  28. openBrace.begin(), openBrace.end(),
  29. closeBrace.begin(),
  30. inserter(o2c, o2c.begin()),
  31. [](const char open, const char close){return make_pair(open, close);}
  32. );
  33. return o2c;
  34. }());
  35. return o2c;
  36. }
  37.  
  38. bool checkBraces (const string& e){
  39. vector<char> s;
  40. for(const char b: e)
  41. if(string::npos!=openBrace.find(b))
  42. s.push_back(openToCloseBrace().at(b));
  43. else if(string::npos!=closeBrace.find(b) && (!s.empty()) && b==s.back())
  44. s.pop_back();
  45. else return false;
  46. return s.empty();
  47. }
  48.  
  49. int main() {
  50. cout<<boolalpha;
  51. transform(
  52. bracesExpressionExamples.begin(),
  53. bracesExpressionExamples.end(),
  54. ostream_iterator<bool>(cout, "\n"),
  55. checkBraces);
  56. return 0;
  57. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
true
false
false
false
false
true
true