码迷,mamicode.com
首页 > 其他好文 > 详细

Sicily 9456. Clumsy Cows

时间:2015-03-30 09:22:36      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:sicily

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 is sufficiently clumsy (due to her large hooves) that 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 are several ways to define what it means for a string of parentheses to be "balanced".  Perhaps the simplest definition is that there must be the same total number of (‘s and )‘s, and for any prefix of the string, there must be at least as many (‘s as )‘s. 

 For example, the following strings are all balanced:

()

(())

()(()())

 while these are not:

)(

())(

((())))

Input

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

Output

 Line 1: A single integer giving the minimum number of parentheses that must be toggled to convert the string into a balanced string.

Sample Input

())(

Sample Output

2

Hint

 The last parenthesis must be toggled, and so must one of the two middle right parentheses.

Problem Source

USACO 2012.11/2013年每周一赛第十三场

非常有趣的一道题目:0s:

#include <stdio.h>
#include <string.h>

//这道题我的思路就是,用right记录没有匹配到的')',用left动态记录匹配值,是'('加一,是')'减一,然后当left小于零的时候right就要加一(说明出现了一个单独的')'),然后将left清零
//注意输入的括号数目确保是偶数

char a[100005];

int main() {
    int right = 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");//如果两者为零说明全部匹配
        } else if (left > 0) {
            printf("%d\n", left / 2);//这种情况说明只剩下'(',只需要把其中一半的'('调转即可
        }
    } else {
        if (left == 0) {
            printf("%d\n", right / 2);//跟上面的情况差不多
        } else if (left > 0) {
            if (left == right) {
                if (left % 2 == 0) {//这种情况就是这种'))))((((',把其中一种括号全部调转即可
                    printf("%d\n", left);
                } else {
                    printf("%d\n", left + 1);//这种情况是')))))((((('这种情况除了把其中一种括号全部调转外,还要额外调转一个另外一种的括号
                }
            } else {
                if (left % 2 == 0) {//这种情况是'))))))(((('自家括号跟自家匹配,各调转一半
                    printf("%d\n", left / 2 + right / 2);
                } else {
                    printf("%d\n", (left - 1) / 2 + (right - 1) / 2 + 2);//')))))(((',自家括号跟自家匹配,然后各拿出一个匹配(就是中间那一对)
                }
            }
        }
    }
    return 0;
}
    


Sicily 9456. Clumsy Cows

标签:sicily

原文地址:http://blog.csdn.net/u012925008/article/details/44738733

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!