POJ3991 HDU3351 UVALive4733 Seinfeld水题

Posted tigerisland45

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3991 HDU3351 UVALive4733 Seinfeld水题相关的知识,希望对你有一定的参考价值。

Seinfeld
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1551 Accepted: 722

Description

I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple problems look difficult and complex problems look easy. But, alas, not for this one.
You’re given a non empty string made in its entirety from opening and closing braces. Your task is to find the minimum number of “operations” needed to make the string stable. The definition for being stable is as follows:

An empty string is stable.

If S is stable, then {S} is also stable.

If S and T are both stable, then ST (the concatenation of the two) is also stable.

All of these strings are stable: {}, {}{}, and {{}{}}; But none of these: }{, {{}{, nor {}{.
The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa.

Input

Your program will be tested on one or more data sets. Each data set is described on a single line. The line is a non-empty string of opening and closing braces and nothing else. No string has more than 2000 braces. All sequences are of even length.
The last line of the input is made of one or more ’-’ (minus signs.)

Output

For each test case, print the following line:

k. N
Where k is the test case number (starting at one,) and N is the minimum number of operations needed to convert the given string into a balanced one.

Sample Input

}{
{}{}{}
{{{}
---
Sample Output

  1. 2
  2. 0
  3. 1

Source

anarc 2009

问题链接POJ3991 HDU3351 UVALive4733 Seinfeld
问题简述:(略)
问题分析
????给定一个数量为偶数的括号字符串,问需要修改多少个括号才能使得括号匹配为合法。
????从左往右看,如果出现一个没有左括号与之匹配的右括号,那么它就一定需要修改为左括号。虽然说,匹配问题通常需要使用堆栈,其实这个题是不需要堆栈的,因为只有左右两种括号,做个统计匹配就可以了。最后剩下的没有匹配的左括号只需要修改其中的一半也就都匹配了。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序如下:

/* POJ3991 HDU3351 UVALive4733 Seinfeld */

#include <stdio.h>

#define N 2000
char s[N + 1];

int main(void)
{
    int caseno = 0, left, ans, i;
    while(~scanf("%s", s) && s[0] != '-') {
        ans = left = 0;
        for(i = 0; s[i]; i++) {
            if(s[i] == '{')
                left++;
            else {
                if(left > 0)
                    left--;
                else
                    ans++, left = 1;
            }
        }
        ans += left / 2;

        printf("%d. %d
", ++caseno, ans);
    }

    return 0;
}

以上是关于POJ3991 HDU3351 UVALive4733 Seinfeld水题的主要内容,如果未能解决你的问题,请参考以下文章

POJ3994 HDU3354 UVALive4736 Probability One水题

POJ1598 ZOJ1315 HDU1606 UVA409 UVALive5493 Excuses, Excuses!文本

POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot MotionDFS+BFS

bzoj千题计划210:bzoj2642 | Poj3968 | UVALive 4992| hdu 3761 Jungle Outpost

HDU 1532||POJ1273:Drainage Ditches(最大流)

POJ 3991 Seinfeld