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 路面修整 一开始没有想到离散化.然后离散化之后就 ...
随机推荐
- oracle--DG监控脚本
conn sys@oracle01 as sysdba column dest_name format a30 column destination format a20 column MEMBER ...
- Windows10 WSL下 龙芯3A 交叉编译环境搭建
记个流水账避免事后忘记怎么搭建的 - - 环境 Key Value 系统 Windows10 WSL系统 Ubuntu 下载工具链 http://www.loongnix.org/index.php/ ...
- Adams宏导出
var set var=ip integer_value=1 var set var=macro_name str="" for variable_name=the_macro o ...
- Qt 绘制图表 - Qt Charts版
一.前言 自从 Qt 发布以来,给广大跨平台界面研发人员带来了无数的福利.但是Qt自己却一直没有提供自带的图表库,这就使得 QWT.QCustomPlot 等第三方图表库有了巨大的生存空间,为了降低开 ...
- MySQL8的密码策略
解释: 由于valiadte_password策略.密码强度需要非常高,所以有时候密码都无法成功修改.了解完下面变量就能解决了. validate_password.policy:密码策略,检查用户的 ...
- 利用mysql的LOAD DATA INFILE的功能读取客户端文件
前言:今天在浏览某知论坛时,看到某大佬在渗透过程中使用伪造的MySQL服务端读取客户端文件,此大佬在利用过程中描述得不是很详细,作为小白的我看不懂啊,由此产生了此篇文章. 某大佬文章:https:// ...
- mongodb 更新数据时int32变为double的解决办法
场景: 在命令手动的修改签到表的整型字段synState,multi参数是可以更新多条,如果是false则更新一条. db.getCollection("ClassRecordOneD ...
- 剑指offer 栈的压入和弹出
题目描述输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...
- win10系统本地iis或nginx服务器部署vue.js项目
1.前端框架一般依赖node.js,我们首先要安装node.js.请参考: http://www.cnblogs.com/wuac/p/6381819.html to:安装好node.js后npm也安 ...
- 深入解析 Go 中 Slice 底层实现
原文: https://halfrost.com/go_slice/ 切片是 Go 中的一种基本的数据结构,使用这种结构可以用来管理数据集合.切片的设计想法是由动态数组概念而来,为了开发者可以更加 ...