【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.
输入输出样例
())(
2
说明
The last parenthesis must be toggled, and so must one of the two middle right parentheses.
陷入dp的泥潭无法自拔,然后终于在题解中看到了一个真·贪心。
其实可以这样贪:只需保证每一个前缀左括号数目大于等于右括号的数目即可。
这样的话有两种贪法(但其实是一种而已?)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> const int MAXN = 100000 + 10; char str[MAXN];
int n;
int cnt;
int num; int main()
{
freopen("data.txt", "r", stdin);
scanf("%s", str + 1);
n = strlen(str + 1);
for(int i = 1;i <= n;i ++)
{
if(str[i] == '(' )num ++;
else num--;
if(num < 0)cnt++,num+=2;
}
printf("%d", cnt + num/2);
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> const int MAXN = 100000 + 10; char str[MAXN];
int n;
int cnt;
int num; int main()
{
freopen("data.txt", "r", stdin);
scanf("%s", str + 1);
n = strlen(str + 1);
for(int i = 1;i <= n;i ++)
{
if(str[i] == '(' )num ++;
else if(str[i] == ')' && num > 0)num--;
else cnt++,num++;
}
printf("%d", cnt + num/2);
return 0;
}
【P3056】【USACO12NOV】笨牛Clumsy Cows的更多相关文章
- 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows
P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...
- USACO Clumsy Cows
洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows 洛谷传送门 JDOJ 2323: USACO 2012 Nov Silver 1.Clumsy Cows JDOJ传送门 Desc ...
- 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 ...
- [Swift]LeetCode1006. 笨阶乘 | Clumsy Factorial
Normally, the factorial of a positive integer n is the product of all positive integers less than or ...
- LeetCode竞赛题:笨阶乘(我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。)
通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.相反,我们设计了一个笨 ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...
- 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows
P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...
随机推荐
- 如何在window和mac下查找数据库
1. mac 下终端使用步骤 cd /Applications/xampp/bin ./mysql -u root 2. window CMD命令中执行步骤 D: cd D:/xampp/mysql ...
- VS2010-MFC(对话框:为控件添加消息处理函数)
转自:http://www.jizhuomi.com/software/156.html MFC为对话框和控件等定义了诸多消息,我们对它们操作时会触发消息,这些消息最终由消息处理函数处理.比如我们点击 ...
- iOS开发UITableView的动画cell
1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...
- Mac OS X 下有关adb相关问题
一.什么是adb? ADB的全称是Android Debug Bridge,用来调试Android程序的,白话点就是debug工具! 位置:一般下载Android的SDK时候在platform-too ...
- spring boot 中 rabbit mq基础例子
1.先安装ELANG,再按照RabbitMQ 2.打开RabbitMQ控制台:rabbit command prompt 1.设置elang的路径:set ERLANG_HOME=D:\work_pr ...
- Error: Could not link: /usr/local/share/doc/homebrew
mac 执行brew update 报错 Error: Could not link: /usr/local/share/doc/homebrew 更新brew,报错 Error: Could not ...
- Netty TCP粘包/拆包问题《二》
1.DelimiterBasedFrameDecoder:是以分隔符作为结束标志进行解决粘包/拆包问题 代码: EchoClient:客户端 /* * Copyright 2012 The Netty ...
- [Swoole系列入门教程 5] UDP协议和demo
• 客户端服务端没有任何联系 • 指定地址跟端口,不关心消息是否发送成功 • 心跳检测不能影响到客户端• udp建立长连接
- 廖雪峰Java9正则表达式-2正则表达式进阶-5非贪婪匹配
1.贪婪匹配 问题:给定一个字符串表示的数字,判断该数字末尾0的个数? "123000": 3个0 "10100": 2个0 "1001": ...
- Makefile知识点
1.标准Makefile文件示例如下: #把.o .C .cxx .cpp .hxx .h添加到后缀列表中. .SUFFIXES: .o .C .cxx .cpp .hxx .h #设置变量并赋值,方 ...