? 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows

P3056 [USACO12NOV]笨牛Clumsy Cows

题目描述

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:

)( ())( ((())))

给出一个偶数长度的括号序列,问最少修改多少个括号可以使其平衡。

输入输出格式

输入格式:

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

输出格式:

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

输入输出样例

输入样例#1: 复制

())(

输出样例#1: 复制

2

说明

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
using namespace std;
char s[N];
int head,tail,ans;
int main(){
    scanf("%s",s);
    head=tail=0;
    int len=strlen(s);
    for(int i=0;i<len;i++){
        if(s[i]==‘(‘) tail++;
        else {
            if(tail>head) tail--;
            else   tail++,ans++;
        }
    }
    if(tail!=head) ans+=(tail-head)/2;
    printf("%d",ans);
}
时间: 2024-08-07 23:56:37

? 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows的相关文章

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads co

贪心 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

[USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题目描述 FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges. The contest organ

洛谷P1588 丢失的牛

P1588 丢失的牛 158通过 654提交 题目提供者JOHNKRAM 标签USACO 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 答案下载下来是对的,但过不- 此题卡stl的queue? 假的编译器... 怎么A不了 题目描述 FJ丢失了他的一头牛,他决定追回他的牛.已知FJ和牛在一条直线上,初始位置分别为x和y,假定牛在原地不动.FJ的行走方式很特别:他每一次可以前进一步.后退一步或者直接走到2*x的位置.计算他至少需要几步追上他的牛. 输入输出

洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landma

洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to spend their free time. Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landma

洛谷P1211 [USACO1.3]牛式 Prime Cryptarithm

P1211 [USACO1.3]牛式 Prime Cryptarithm 187通过 234提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 题面错误 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x     ** ---------- *** *** ---------- **** (请复制到记事本) 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学

洛谷——P1588 丢失的牛

P1588 丢失的牛 题目描述 FJ丢失了他的一头牛,他决定追回他的牛.已知FJ和牛在一条直线上,初始位置分别为x和y,假定牛在原地不动.FJ的行走方式很特别:他每一次可以前进一步.后退一步或者直接走到2*x的位置.计算他至少需要几步追上他的牛. 输入输出格式 输入格式: 第一行为一个整数t(≤10),表示数据组数:接下来每行包含一个两个正整数x和y(0<x,y≤10^5),分别表示FJ和牛的坐标. 输出格式: 对于每组数据,输出最少步数. 输入输出样例 输入样例#1: 1 5 17 输出样例#

l洛谷——P1211 [USACO1.3]牛式 Prime Cryptarithm

P1211 [USACO1.3]牛式 Prime Cryptarithm 题目描述 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. *** x ** ---------- *** *** ---------- **** (请复制到记事本) 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找

洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between nearby fields. Taking this into account, he wants to plant enough grass in each of his fields not only for the cows situated initially in that field, but