SRM521 Div1 Easy(250), Div2 Medium(500) MissingParentheses
開いていないのに閉じている括弧の個数を足していき、最後に開いたままの括弧の分を足す。
#include <string> #include <vector> using namespace std; class MissingParentheses{public: int countCorrections( string par ) { int ans = 0; int c = 0; for ( int i=0; i<(int)par.size(); i++ ) if ( par[i]=='(' ) c++; else if ( c > 0 ) c--; else ans++; ans += c; return ans; }};