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. .Net 安装aliyun-oss

    NuGet安装 如果您的Visual Studio没有安装NuGet,请先安装 NuGet. 安装好NuGet后,先在Visual Studio中新建或者打开已有的项目,然后选择工具 > NuG ...

  2. Python正则表达式初识(三)

    前几天给大家分享了Python正则表达式基础(一)和Python正则表达式基础(二),感兴趣的小伙伴可以点击进去学习,今天继续给大家分享Python正则表达式基础. 1.正则表达式特殊字符“+”,其代 ...

  3. 紫书 例题 9-12 UVa 12186 (树形dp)

    这道题还是比较简单的,对于当前节点,算出每个儿子需要的人数 然后再算出当前节点需要多少个人数,然后排个序加上去就好了. #include<cstdio> #include<vecto ...

  4. 2018 NOIP备战计划

    2018 NOIP目标 (1)刷完紫书数论习题 (2)听51nod讲座和习题,根据其知识结构来备战. (3)刷完紫书动规 (4)初赛前两个星期左右开始复习 刷紫书动规的时候感觉偏难,进步缓慢.应该自己 ...

  5. 通用查询实现方案(可用于DDD)[附源码] -- 设计思路

    原文:通用查询实现方案(可用于DDD)[附源码] -- 设计思路 [声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3988592.html).   ...

  6. Nagle和Cork

    我觉得这篇讲的不错. http://blog.csdn.net/c_cyoxi/article/details/8673645 Nagle算法的基本定义是任意时刻,最多只能有一个未被确认的小段. 关闭 ...

  7. docker on spark

    从docker 仓库 pull 镜像 docker pull sequenceiq/spark:1.4.0 构建 docker 镜像 docker build –rm -t sequenceiq/sp ...

  8. 关于app.FragmentManager和v4包的FragmentPagerAdapter冲突

    这几天发现一个问题我用getFragmentManager()得到FragmentManager不能放到FragmentPagerAdapter里面去.由于FragmentPagerAdapter里面 ...

  9. JS排序的运用

    排序是一个非常实用的功能,队列也是一样实用. 有时候项目中就是会用到它. 举个例子,队列的运用,比如刷小礼物,接收方,会受到很多用户的礼物.为了公平起见,要一个个的显示出礼物效果.这个时候就需要队列了 ...

  10. BZOJ 1232 Kruskal

    思路: 跟昨天的考试题特别像-.. 就是裸的Kruskal 把边权设为连接的两个点的点权之和加上边权*2 搞定 //By SiriusRen #include <cstdio> #incl ...