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

Input
((?))
Output
4
Input
??()??
Output
7

Note

For the first sample testcase, the pretty substrings of s are:

  1. "(?" which can be transformed to "()".
  2. "?)" which can be transformed to "()".
  3. "((?)" which can be transformed to "(())".
  4. "(?))" which can be transformed to "(())".

For the second sample testcase, the pretty substrings of s are:

  1. "??" which can be transformed to "()".
  2. "()".
  3. "??()" which can be transformed to "()()".
  4. "?()?" which can be transformed to "(())".
  5. "??" which can be transformed to "()".
  6. "()??" which can be transformed to "()()".
  7. "??()??" 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 贪心+思维的更多相关文章

  1. codeforces 893D Credit Card 贪心 思维

    codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...

  2. Codeforces 671A Recycling Bottles(贪心+思维)

    题目链接:http://codeforces.com/problemset/problem/671/A 题目大意:给你两个人的位置和一个箱子的位置,然后给出n个瓶子的位置,要求让至少一个人去捡瓶子放到 ...

  3. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  4. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  5. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  6. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  7. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  8. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  9. T - Posterized(贪心思维)

    Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...

随机推荐

  1. IDEA自学

    使用Eclipse很长时间了,想换个IDE用,都说IDEA好用,今天试试 百度了一下IDEA,了解到IDEA社区版免费,上百度,下载个社区版(exe,zip两种)懒人选择exe 手动安装别怕安错,只管 ...

  2. html+css+dom补充

    补充1:页面布局 一般像京东主页左侧右侧都留有空白,用margin:0 auto居中,一般.w. <!DOCTYPE html> <html lang="en"& ...

  3. Python基础总结之初步认识---clsaa类(上)。第十四天开始(新手可相互督促)

    最近的类看着很疼,坚持就是胜利~~~ python中的类,什么是类?类是由属性和方法组成的.类中可能有很多属性,以及方法. 我们这样定义一个类: 前面是class关键字 后面school是一个类的名字 ...

  4. java并发编程(十七)----(线程池)java线程池架构和原理

    前面我们简单介绍了线程池的使用,但是对于其如何运行我们还不清楚,Executors为我们提供了简单的线程工厂类,但是我们知道ThreadPoolExecutor是线程池的具体实现类.我们先从他开始分析 ...

  5. mpvue微信小程序项目踩坑记录

    1.mpvue入门教程, http://mpvue.com/mpvue/quickstart.html # . 先检查下 Node.js 是否安装成功 $ node -v v8.9.0 $ npm - ...

  6. 用 程序 解决 windows防火墙 的 弹窗 问题

    今天用户反馈了一个问题,运行程序弹了个框 这个只有程序第一次运行会出来,之后就不会了. 当然改个程序名字,又会弹出来. 强烈怀疑是写到了注册表,果然被我找到了. “HKEY_LOCAL_MACHINE ...

  7. exported function xxx should have comment or be unexported

    0x00 问题 exported function xxx should have comment or be unexported. 0x01 解决 https://golang.org/s/sty ...

  8. 集合中Iterator迭代器的使用以及实现原理。

    collection集合元素通用的获取方式,在取之前先要判断集合中有没有元素,如果有就把这个元素取出来,继续在判断,如果还有就再取出来,一直把集合中的元素全取出来,这种去出方式叫做迭代. 迭代器的作用 ...

  9. 这些用来审计 Kubernetes RBAC 策略的方法你都见过吗?

    原文链接:这些用来审计 Kubernetes RBAC 策略的方法你都见过吗? 认证与授权对任何安全系统来说都至关重要,Kubernetes 也不例外.即使我们不是安全工作人员,也需要了解我们的 Ku ...

  10. 开源音乐下载神器XMusicDownloader更新,支持歌单一键下载,支持无损音乐

    开源音乐下载神器XMusicDownloader更新啦,新增网易.腾讯音乐歌单歌曲.歌手歌曲.专辑歌曲一键下载,同时支持下载flac无损音乐. 功能 V1.0 功能开源工具软件XMusicDownlo ...