USACO Clumsy Cows

Luo Gu P3056 [USACO12NOV] stupid cow Clumsy Cows

Luo Gu Portal

JDOJ 2323: USACO 2012 Nov Silver 1.Clumsy Cows

JDOJ Portal

Description

Problem 1: Clumsy Cows [Brian Dean, 2012]

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

OUTPUT DETAILS:

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

Subject to the effect:

Given a sequence of even length of brackets, braces least ask how many modifications can be allowed to equilibrate.

I explain a little bit Shajiao brackets balanced, that is, an equal number of left and right parentheses.

answer:

This question is practiced hand problem stack structure.

In fact, that Italy is also very simple, it is a simulation, but we can stack data structure makes this thing becomes more clear and concise.

We can consider the idea "Diminshing music" (Self nouns

Left parenthesis is the normal push, if you encounter a right parenthesis and the stack is not empty on the pop-up to match him, if you encounter a right parenthesis and the stack is empty, and it shows that there is nothing to match him, we need to change this thing into a left parenthesis pushed onto the stack, while ans ++.

Note that the last time, we have to judge to see if the stack is empty, if not empty words of explanation or unbalanced, then also need to ans + 2 divided by the number of elements in the stack.

The principle is very simple /

Code:

#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
char s[100001];
stack<char> st;
int ans;
int main()
{
    scanf("%s",s+1);
    int len=strlen(s+1);
    for(int i=1;i<=len;i++)
    {
        if(s[i]=='(')
            st.push(s[i]);
        if(s[i]==')')
        {
            if(st.empty())
            {
                st.push('(');
                ans++;
            }
            else
                st.pop();
        }
    }
    if(!st.empty())
        ans+=st.size()/2;
    printf("%d",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11294163.html