bzoj3016[usaco2012 Nov]clumsy cows*

Source: Internet
Author: User

bzoj3016[usaco2012 Nov]clumsy Cows

Test instructions

Given a sequence of parentheses of length n, each modification can modify the parentheses of a position, if the parenthesis is ' (', then modified to ') ', if this parenthesis is ') ', then modify to ' (', ask at least how many changes are made to make the original bracket sequence valid. Where: () is lawful; Ais legal, then ( A) is lawful; ABare legal, then ABis legal. n≤100000.The following:The idea is simple, but I can't know its correctness: sweep the sequence once, if you encounter a closing parenthesis and the number of closing parentheses is greater than the number of opening parentheses, change the closing parenthesis to an opening parenthesis. After sweep statistics, if the left parenthesis more then half of the left parenthesis will be changed to a closing parenthesis.Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 using namespacestd;6 7 CharChintS,ans;8 intMain () {9Ch=GetChar ();Ten      while(ch!='\ n'){ One         if(ch=='(') s++;if(ch==')') {s--;if(s<0) s+=2, ans++;} Ch=GetChar (); A     } -ans+=s/2; printf"%d", ans);return 0; -}

20161019

bzoj3016[usaco2012 Nov]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.