USACO Clumsy Cows
洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows
JDOJ 2323: USACO 2012 Nov Silver 1.Clumsy Cows
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.
题目大意:
给出一个偶数长度的括号序列,问最少修改多少个括号可以使其平衡。
我再多解释一下啥叫括号平衡,就是左括号和右括号的数量相等。
题解:
这道题是栈结构的练手题。
其实题意也很简单,就是一个模拟,但是我们可以用栈的数据结构使得这个东西变得更加简洁明了。
我们可以考虑一下“消消乐”的思想(自编名词)
就是左括号正常进栈,如果碰到右括号且栈不为空就弹出来跟他匹配,如果碰到右括号而且栈空了,那就说明没有东西和他匹配,我们就需要把这个东西改成左括号压进栈,同时ans++。
注意,最后的时候,我们还要判一下这个栈是否为空,如果不为空的话说明还是不平衡,那么还需要把ans+栈内元素个数除以2.
原理很简单了/
代码:
#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;
}
USACO Clumsy Cows的更多相关文章
- BZOJ3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 71 Solved: 52[Submi ...
- 3016: [Usaco2012 Nov]Clumsy Cows
3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 91 Solved: 69[Submi ...
- 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows
P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...
- 【P3056】【USACO12NOV】笨牛Clumsy Cows
P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...
- 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3016 之前yy了一个贪心,,,但是错了,,就是枚举前后对应的字符(前面第i个和后面第i个)然后相同答 ...
- USACO Milking Cows
思路: 脑抽了,一看题目,这不就是线段树么,离散化区间合并..最终发现我并不会写...于是看了下题目范围10^6...模拟水之..每个区间左端点+1,右端点-1,从左到右扫一下就行了... 代码: / ...
- BZOJ 3016 [Usaco2012 Nov]Clumsy Cows:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3016 题意: 给你一个括号序列,问你至少修改多少个括号,才能使这个括号序列合法. 题解: ...
- BZOJ USACO 银组 水题集锦
最近刷银组刷得好欢快,好像都是水题,在这里吧他们都记录一下吧(都是水题大家一定是道道都虐的把= =)几道比较神奇的题到时再列出来单独讲一下吧= =(其实我会说是BZOJ蹦了无聊再来写的么 = =) [ ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
随机推荐
- 解决win10下IIS下"对路径...的访问被拒绝
解决方案如下: 1.右击文件夹,安全,添加IUSR和IIS_IUSRS安全控制权限. 2.右击文件夹,常规,查看是否只读.将只读属性去掉.
- ES6中ArrayBuffer与计算机字节序
1.什么事字节序? 字节序指的是数值在内存中的表示方式. const buffer = new ArrayBuffer(16); const int32View = new Int32Array(bu ...
- 深入理解C语言 - 指针使用的常见错误
在C语言中,指针的重要性不言而喻,但在很多时候指针又被认为是一把双刃剑.一方面,指针是构建数据结构和操作内存的精确而高效的工具.另一方面,它们又很容易误用,从而产生不可预知的软件bug.下面总结一下指 ...
- Oracle性能调优之虚拟索引用法简介
本博客记录一下Oracle虚拟索引的用法,虚拟索引是定义在数据字典中的伪索引,可以说是伪列,没有修改的索引字段的.虚拟索引的目的模拟索引,不会增加存储空间的使用,有了虚拟索引,开发者使用执行计划的时候 ...
- git add命令行添加文件、文件夹以及撤销文件add的方法
1.添加某个文件类型到暂存区,比如所有的 .html 文件. git add *.html 2.添加某个文件或者某个文件夹中的某个文件到暂存区 ,比如 index 下的 index.htm ...
- spring-retry 重试机制的使用
场景:由于网络抖动原因,或者其他原因,需要对代码重新执行,这个就需要重试了. import org.springframework.context.annotation.Configuration; ...
- 《 .NET并发编程实战》阅读指南 - 第6章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- typora安装自定义主题小计
我写MarkDown一直使用VSCode,后来因为有导出PDF的需求,就用了typora(含书签) PS:VSCode的导出PDF虽然也可以,但不含书签 typora的默认主题是github,虽然还可 ...
- ******可用 SpringBoot 项目打包分开lib,配置和资源文件
spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...
- LoadRunner 11 的兼容问题及权限问题
1.LoadRunner 11 在服务器系统中可能出现 不兼容问题. 要对 安装目录bin\LRLauncherApp.exe 和 bin\wlrun.exe 右键属性兼容性(视系统定). 2.如果填 ...