洛谷 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.
输入输出样例
说明
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的更多相关文章
- 【P3056】【USACO12NOV】笨牛Clumsy Cows
P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- 洛谷P1522 [USACO2.4]牛的旅行 Cow Tours
洛谷P1522 [USACO2.4]牛的旅行 Cow Tours 题意: 给出一些牧区的坐标,以及一个用邻接矩阵表示的牧区之间图.如果两个牧区之间有路存在那么这条路的长度就是两个牧区之间的欧几里得距离 ...
- 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷——P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
- 洛谷 P2862 [USACO06JAN]把牛Corral the Cows
P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...
随机推荐
- .Net中字典的使用
/// <summary> /// 获取用户市信息 /// </summary> /// <param name="CustomerId">&l ...
- Python列表插入字典(转)
https://blog.csdn.net/qq_29721419/article/details/70310183
- [ Java ][ Eclipse ] 停止讓 Eclipse 跳出 Password Required
stackoverflow 上,問題的解決方式: http://stackoverflow.com/questions/4713890/how-to-disable-programmatically- ...
- 2D上下文
js中说明的上下文表示的意思为C++中作用域(个人理解),因此2D上下文说明的是这个2D的作用域 像素:用来描述图片清晰度的小矩阵 填充和描边 填充:context.fillStyle = " ...
- android反编译odex文件
关于android的反编译工具,相信大家并不陌生 如APK-TOOL,dex2jar APK-TOOL 用于反编译出布局文件 下载地址http://code.google.com/p/android- ...
- Struts(18)标签
控件标签 Struts 2 的标签有一组标签.更easy控制流程页面运行.下面是重要的Struts2控制标签列表: if /else 标签: 这些标签运行可在每一种语言找到的一种基本条件流程. 'If ...
- 改动UINavigationBar (导航栏)上NavigationBarItem 的字体大小和颜色的用法
//创建一个左边button UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"<" ...
- [C/C++标准库]_[0基础]_[使用fstream合并文本文件]
场景: 1. 就是合并文本文件,而且从第2个文件起不要合并第一行. 2. 多加了一个功能,就是支持2个以上的文件合并. 3. 问题: http://ask.csdn.net/questions/192 ...
- JAVA基础针对自己薄弱环节总结02(循环)
循环 A:水仙花. classShuiXianHua { public static void main(String[] args) { for(int i=101;i<1000;i++) { ...
- 在gridview里查找模板里的button控件
这个问题,真是搞了我1天,这次记住他 第一种方法: protected void GridView1_RowCommand(object sender, GridViewCommandEventArg ...