/* Topcoder SRM 688, D2, 250-pointer: ParenthesesDiv2Easy * James S. Plank * Tue Sep 6 00:42:57 EDT 2016 */ #include #include #include #include #include #include #include #include #include #include using namespace std; class ParenthesesDiv2Easy { public: int getDepth(string s); }; int ParenthesesDiv2Easy::getDepth(string s) { int i; /* We will use i to run through s from left to right. */ int p; /* P will be our counter that records the depth at s[i]. */ int rv; /* This is our return value -- the max depth. */ rv = 0; p = 0; /* Increment p when you encounter a left paren, and record the maximum depth. Decrement p when you encounter a right paren. */ for (i = 0; i < s.size(); i++) { if (s[i] == '(') { p++; if (p > rv) rv = p; } else { p--; } } return rv; }