Sicily 9456. Clumsy cows

Source: Internet
Author: User

9456. Clumsy cows Constraints

Time limit:1 secs, Memory limit:256 MB

Description

bessie the cow is trying To type a balanced string of parentheses into her new laptop, but she's sufficiently clumsy (due to her large hooves) tha T she keeps mis-typing characters.  please help her by computing the minimum number of characters in the string that One must reverse (e.g., changing a left parenthesis to a right parenthesis, or vice versa) so that the string would become Balanced.

  there is several ways to define what it means for a string of parentheses to be" balanced ".  perhaps The simplest definition is that there must being the same total number of (' s and) ' s, and for any prefix of the string, the Re must is at least as many (' s as) ' s. 

For example, the following strings is all balanced:

()

(())

()(()())

While these is not :

)(

())(

((())))

Input

* Line 1: A string of parentheses of even length at the most 100,000 characters.

Output

Line 1: A single integer giving the minimum number of parentheses that must being toggled to convert the string into A Balanc Ed String.

Sample Input
())(
Sample Output
2
Hint

The last parenthesis must is toggled, and so must one of the "the" middle right parentheses.

Problem Source

Usaco 2012.11/2013 Annual 13th game of the week

A very interesting topic: 0s:

< Span style= "font-family: ' Hiragino Sans GB W3 ', ' Hiragino Sans GB ', Arial,helvetica,simsun, song body; font-size:16px; Line-height:28px ">

#include <stdio.h> #include <string.h>//This question my idea is, with right record does not match to ') ', with left dynamic record match value, is ' (' plus one, is ') ' minus one,    Then when left is less than 0, right will add one (indicating that a separate ') '), and then leave clear 0//Note the number of parentheses entered ensures that it is even Char A[100005];int main () {int. = 0, left = 0;    Gets (a);        for (int i = 0; i < (int) strlen (a); i++) {if (a[i] = = ' (') {left++;        } else {left--;            } if (left < 0) {right++;        left = 0;  }} if (right = = 0) {if (left = = 0) {printf ("0\n");//If both are zero description all match} else if (left > 0)            {printf ("%d\n", LEFT/2);//This condition indicates that only the ' (', only half of the ' (') ' (' Reverse '}} else {if (left = = 0) {                printf ("%d\n", RIGHT/2);//the same as above} else if (left > 0) {if (left = right) {                if (left% 2 = = 0) {//This is the case))) (((') the%d\n of one of the parentheses can be printf ("The", "leave"); } else {printf ("%d\n", left + 1) (//This case is ')))))In addition to reversing one of the brackets, the other is an additional parenthesis}} else {if (left% 2 = = 0) {//This case is ')))))))))))) ((                (' Home brackets match your own, half of printf ("%d\n", LEFT/2 + RIGHT/2);                 } else {printf ("%d\n", (left-1)/2 + (RIGHT-1)/2 + 2) (//')))) (('), the home brackets match your home, then each one matches (that is, the middle pair).     }}}} return 0;}


Sicily 9456. Clumsy cows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.