CodeForces 917A The Monster 贪心+思维
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him.
Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.
A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally:
- Empty string is a correct bracket sequence.
- if s is a correct bracket sequence, then (s) is also a correct bracket sequence.
- if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.
Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≤ l ≤ r ≤ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.
Joyce doesn't know anything about bracket sequences, so she asked for your help.
Input
The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≤ |s| ≤ 5000).
Output
Print the answer to Will's puzzle in the first and only line of output.
Example
((?))
4
??()??
7
Note
For the first sample testcase, the pretty substrings of s are:
- "(?" which can be transformed to "()".
- "?)" which can be transformed to "()".
- "((?)" which can be transformed to "(())".
- "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are:
- "??" which can be transformed to "()".
- "()".
- "??()" which can be transformed to "()()".
- "?()?" which can be transformed to "(())".
- "??" which can be transformed to "()".
- "()??" which can be transformed to "()()".
- "??()??" which can be transformed to "()()()".
可以将?变成)或(,问括号匹配数量
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#define debug(a) cout << #a << " = " << a <<endl
using namespace std;
int main() {
string s;
while( cin >> s ) {
int ans = ;
for(int i=;i<s.length();i++) { //枚举所有情况下?对字符串的影响
int cnt1 = ,cnt2 = ; //cnt1把?看作)未匹配数量,cnt2把?看作(未匹配数量
for(int j=i;j<s.length();j++) {
if( s[j] == '(' ) {
cnt1 ++;
cnt2 ++;
} else if( s[j] == ')' ) {
cnt1 --;
cnt2 --;
} else {
cnt1 --;
cnt2 ++;
}
if( cnt1 < && cnt2 > ) {
cnt1 += ; //当把?看成)导致cnt1<0时再把?看成(,这样未匹配数量又为1了
//cnt2 --;
}
if( cnt2 < ) {
break;
}
if( cnt1 == ) {
ans ++;
}
}
//debug(i);
//debug(ans);
}
cout << ans << endl;
}
return ;
}
CodeForces 917A The Monster 贪心+思维的更多相关文章
- codeforces 893D Credit Card 贪心 思维
codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...
- Codeforces 671A Recycling Bottles(贪心+思维)
题目链接:http://codeforces.com/problemset/problem/671/A 题目大意:给你两个人的位置和一个箱子的位置,然后给出n个瓶子的位置,要求让至少一个人去捡瓶子放到 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- T - Posterized(贪心思维)
Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...
随机推荐
- 【Python】Django 的邮件引擎用法详解!!(调用163邮箱为例)
1. send_mall()方法介绍 位置: 在django.core.mail模块提供了send_mail()来发送邮件. 方法参数: send_mail(subject, message, fro ...
- GStreamer流媒体知识介绍
GStreamer框架 1.GStreamer是什么? 众所周知,Microsoft's Windows和Apple's MacOS对多媒体设备.多媒体创作.播放和实时处理等方面都有很好的支持,而Li ...
- 完全零基础在Linux中安装 JDK
完全零基础在Linux中安装 JDK 总体思路:先确定没有Java程序了 — 然后创建相应路径文件夹 — 下载JDK — 解压到当前路径 — 自定义文件名称 — 配置环境变量 — 检查是否安装成功 第 ...
- oracle 断电启动失败:ORA-00600: internal error code, arguments
转载地址: http://www.2cto.com/database/201312/261602.html 由于服务器断电,启动 oracle 时报 ORA-00600 错误 查看 oracle tr ...
- Activiti6系列(1)- 核心数据库表及字段注释说明
前言 本文是根据<疯狂工作流讲义-Activiti6.0>一书中提取过来的,有兴趣的可以去当当网买这本书,讲的很不错,最后还有实战案例. 虽然是提取过来的,但完全靠手打梳理,觉得有用的小伙 ...
- Python Iterator and Generator
Python Iterator and Generator Iterator 迭代器(Iterator)和可迭代对象(Iterable)往往是绑定的.可迭代对象就是我们平时经常用的list ,st ...
- .netcore持续集成测试篇之Xunit数据驱动测试一
系列目录 Nunit里提供了丰富的数据测试功能,虽然Xunit里提供的比较少,但是也能满足很多场景下使用了,如果数据场景非常复杂,Nunit和Xunit都是无法胜任的,有不少测试者选择自己编写一个数据 ...
- html学习笔记整理
网页 1.网页的组成部分 网页是由文字,图片,视频,音频,输入框,按钮这些元素(也就是html标签)组成. 2.浏览网页常用的五大主流浏览器 谷歌,IE,火狐,欧朋,safari.浏览器的内核(渲染引 ...
- 洛谷 P2158 [SDOI2008]仪仗队
题意简述 给定一个n,求gcd(x, y) = 1(x, y <= n)的(x, y)个数 题解思路 欧拉函数, 则gcd(x, y) = 1(x <= y <= n)的个数 ans ...
- Django2.2中间件详解
中间件是 Django 用来处理请求和响应的钩子框架.它是一个轻量级的.底层级的"插件"系统,用于全局性地控制Django 的输入或输出,可以理解为内置的app或者小框架. 在dj ...