int main() {
    std::string text;
    getline(std::cin, text);   // get the input string

    std::stack <Bracket> opening_brackets_stack;
    for (int position = 0; position < text.length(); ++position) {
        char next = text[position];

        if (next == '(' || next == '[' || next == '{') {  // store the left bracket in the stack
            Bracket parenStart(next, position);
			opening_brackets_stack.push(parenStart);
        }
        else if (next == ')' || next == ']' || next == '}') {  // if right bracket is detected
			if (opening_brackets_stack.empty())   // if the stack is empty, which means no left bracket is stored
			{
				std::cout << (position + 1) << std::endl;  // output the position of this right bracket, break loop
				break;
			}
			else
			{
				Bracket parenStartOut = opening_brackets_stack.top(); // get the latest left bracket, 
				                                                      //remove it from stack
				opening_brackets_stack.pop();   
			    if(!parenStartOut.Matchc(next))  // check if the latest left bracket is matched with
			                                     // the current right bracket
			    {
                    std::cout << (position + 1) << std::endl; // if not, output the position of this right bracket, break loop
				    break;
			    }
			}

        }
		if (position == text.length() - 1)  // if reach the end of the string
		{
			if (!opening_brackets_stack.empty()) // if the stack is not empty, output the position of 
			                                     //left bracket at the end of the stack (the first unmatched one) 
			{
				Bracket firstUnMatched('(',0); // initialization, initial values don't matter here
				while(!opening_brackets_stack.empty())
			    {
				   firstUnMatched = opening_brackets_stack.top();
				   opening_brackets_stack.pop();
			    }
			    std::cout << (firstUnMatched.position + 1) << std::endl;
			}
			else // if stack has no left bracket, which means all the brackets have been matched
			{
				std::cout << "Success" << std::endl;
			}

		}
    }


    return 0;
}
Ejemplo n.º 2
0
int main() {

    bool isBalanced = true;
    int errorPos = 0;

    std::string text;
    getline(std::cin, text);

    std::stack <Bracket> opening_brackets_stack;
    for (int position = 0; position < text.length(); ++position) {
        char next = text[position];

        if (next == '(' || next == '[' || next == '{') {
            // Process opening bracket, write your code here
            Bracket b = Bracket(next, position);
            opening_brackets_stack.push(b);
        }

        if (next == ')' || next == ']' || next == '}') {
            // Process closing bracket, write your code here
           if( opening_brackets_stack.empty() == false )
           {
            Bracket b = opening_brackets_stack.top();
            if ( b.Matchc(next) == false  ) {
              isBalanced = false;
              errorPos = position;
              break;
            } else
                opening_brackets_stack.pop();
           } else 
             { 
               isBalanced = false;
               errorPos = position;
               break;
             }
        }
    }
    
    if(opening_brackets_stack.empty() == false && isBalanced == true) 
    {
      isBalanced = false;
      errorPos = opening_brackets_stack.top().position;
    }

    // Printing answer, write your code here
    if ( isBalanced ) 
    	std::cout << "Success" << "\n";
    else 
        std::cout << errorPos+1 << "\n";
    
    return 0;
}