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=;
int len=strlen(s);
for(int i=;i<len;i++){
if(s[i]=='(') tail++;
else {
if(tail>head) tail--;
else tail++,ans++;
}
}
if(tail!=head) ans+=(tail-head)/;
printf("%d",ans);
}

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

  1. 【P3056】【USACO12NOV】笨牛Clumsy Cows

    P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...

  2. 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]

    题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...

  3. 洛谷P1522 [USACO2.4]牛的旅行 Cow Tours

    洛谷P1522 [USACO2.4]牛的旅行 Cow Tours 题意: 给出一些牧区的坐标,以及一个用邻接矩阵表示的牧区之间图.如果两个牧区之间有路存在那么这条路的长度就是两个牧区之间的欧几里得距离 ...

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

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  5. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows

    一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...

  6. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  7. 洛谷——P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  8. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  9. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

随机推荐

  1. BZOJ 1082 暴搜

    思路: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using ...

  2. Sqoop 的基本架构

    不多说,直接上干货! Sqoop 的基本架构图   注意: Sqoop,只需map task就可以了,因为,它只是做数据传输,不需做数据处理.  

  3. css3 背景过度实现

    代码: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&q ...

  4. 安装个wampserver 环境 执行php

    php代码执行要有相关环境. 在这里推荐一个环境工具.wampserver :内置了下面工具: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5 ...

  5. iOS项目开发实战——学会使用TableView列表控件(二)

    要在iOS开发中使用TableView列表控件,不仅能够直接使用TableViewController作为整个主界面,并且还能够使用TableView控件来实现.使用TableView能够进行很多其它 ...

  6. php中str_repeat函数

    php中str_repeat函数 一.作用 用于repeat str 二.实例:输出菱形 代码: <!DOCTYPE html> <html lang="en"& ...

  7. 一题多解(三)—— Python 字符串的拼接

    1. format def event_log(name, time): print('Event: {}, happens at {}'.format(name, str(time))) 2. 使用 ...

  8. CSU 8月月赛 Decimal 小数化分数

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1303 这个OJ很容易跪所以我贴一下题目 Description 任意一个分数都是有理数,对于任意一 ...

  9. 趣题: 按二进制中1的个数枚举1~2^n (位运算技巧)

    ; ; k <= n; k++){ << k)-,u = << n; s < u;){ ;i < n;i++) printf(-i)&); print ...

  10. HDU 4696 Answers 水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4696 由题意可知 1<=Ci<=2 而且图上一定有环 那么我们可以得出: 只要存在奇环(即Ci=1) ...