洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows

洛谷传送门

JDOJ 2323: USACO 2012 Nov Silver 1.Clumsy Cows

JDOJ传送门

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的更多相关文章

  1. BZOJ3016: [Usaco2012 Nov]Clumsy Cows

    3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 52[Submi ...

  2. 3016: [Usaco2012 Nov]Clumsy Cows

    3016: [Usaco2012 Nov]Clumsy Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 91  Solved: 69[Submi ...

  3.  洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows

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

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

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

  5. 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3016 之前yy了一个贪心,,,但是错了,,就是枚举前后对应的字符(前面第i个和后面第i个)然后相同答 ...

  6. USACO Milking Cows

    思路: 脑抽了,一看题目,这不就是线段树么,离散化区间合并..最终发现我并不会写...于是看了下题目范围10^6...模拟水之..每个区间左端点+1,右端点-1,从左到右扫一下就行了... 代码: / ...

  7. BZOJ 3016 [Usaco2012 Nov]Clumsy Cows:贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3016 题意: 给你一个括号序列,问你至少修改多少个括号,才能使这个括号序列合法. 题解: ...

  8. BZOJ USACO 银组 水题集锦

    最近刷银组刷得好欢快,好像都是水题,在这里吧他们都记录一下吧(都是水题大家一定是道道都虐的把= =)几道比较神奇的题到时再列出来单独讲一下吧= =(其实我会说是BZOJ蹦了无聊再来写的么 = =) [ ...

  9. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

随机推荐

  1. vue.js三种安装方式

    Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...

  2. C# HTTP系列1 HttpWebRequest类

    系列目录     [已更新最新开发文章,点击查看详细] .NET Framework 中 System.Net 命名空间下提供了 HttpWebRequest 和 HttpWebResponse 2个 ...

  3. 用siege测试接口高并发

    siege -c 255 -r 2555 "http://10.1.1.6:3001/decode POST <./api.json" -t 100s

  4. 一次kuberneets evicted的历险

    一.概述 kubernetes 的eviction检测diskpresure,检测的是kubelet的root-dir.kubelet的默认root-dir是/var/lib/kubelet,可以使用 ...

  5. WPF ResourceDictionary XAML资源 c#代码 获取与遍历

    使用C#代码来获取XAML资源,除去正常的FindResource.而且是能查询到资源的对象. 说实话还是很麻烦的. 比如说我现在有一堆静态资源放在xaml的资源中,我想通过绑定的方式来获取. 好比是 ...

  6. 删除链表中的倒数第N个节点

    题目 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: ->->->->, 和 n = . 当删除了倒数第二个节点后,链表变为 -&g ...

  7. 获取Url地址中参数的3种方法【华为云技术分享】

    获取Url的代码如下:window.location.href; 方法一:原生js(假设已经获得了Url地址) var url = 'https://gitbook.cn/gitchat/geekbo ...

  8. virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护

    virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护 两个方面:对应用端 & 对DOM端(渲染准备的计算) 1.将DOM状态的维护工作由系统维护转交给vi ...

  9. 使用kibana给不同的用户创建不同的space

    Elastic安全机制 在很多的情况下,出于安全的原因,我们需要对不同的Kibana用户分配不同的用户权限,这样使得他们之间不能互相访问彼此的资源,同 时他们也应该对不同的索引拥有不同的权限,比如读, ...

  10. [转] vue父组件触发子组件事件

    1. 父组件中获取子组件方法 $children 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <template>     < ...