C. The Monster
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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 "()()()".



//用r记录到当前位置的"("数量,t记录到当前位置的"?"数量,ans记录匹配数量
//出现"(",r++
//出现")",r--
//出现"?",r--,t++ //if(r==0)刚好匹配 ans++
//if(r<0&&t>0) r+=2 t--
//if(r<0&&t<0)break #include<iostream>
#include<string>
#include<fstream>
using namespace std; int main()
{
//freopen("in.txt","r",stdin);
string str;
cin>>str;
int r=0;int t=0;int ans=0;
for(int j=0;j<str.length();j++)
{ r=0;t=0;
for(int i=j;i<str.length();i++)
{
if(str[i]=='(')r++;
else if(str[i]==')')r--;
else r--,t++;
if(r==0)ans++;
else if(r<0&&t>0)r+=2,t--;
else if(r>0) continue;
else if(r<0&&!t)break; }
}
cout<<ans<<endl; return 0;
}




Codeforces Round #459 (Div. 2)C. The Monster的更多相关文章

  1. Codeforces Round #459 (Div. 2)The Monster[匹配问题]

    题意 给一个序列,包含(,),?,?可以被当做(或者),问你这个序列有多少合法的子序列. 分析 n^2枚举每一个子序列,暂时将每个?都当做右括号,在枚举右端点的时候同时记录两个信息:当前左括号多余多少 ...

  2. 【Codeforces Round #459 (Div. 2) C】The Monster

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 左括号看成1 右括号看成-1 设置l,r表示前i个数的和的上下界 遇到 左括号 l和r同时加1 遇到右括号 同时减1 遇到问号 因为 ...

  3. Codeforces Round #459 (Div. 2)

    A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  4. Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学

    B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...

  5. Codeforces Round #328 (Div. 2)_B. The Monster and the Squirrel

    B. The Monster and the Squirrel time limit per test 1 second memory limit per test 256 megabytes inp ...

  6. Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈

    D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  7. Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)

    D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...

  8. Codeforces Round #459 (Div. 2):B. Radio Station

    B. Radio Station time limit per test2 seconds memory limit per test256 megabytes Problem Dsecription ...

  9. Codeforces Round #459 Div. 1

    C:显然可以设f[i][S]为当前考虑到第i位,[i,i+k)的状态为S的最小能量消耗,这样直接dp是O(nC(k,x))的.考虑矩阵快速幂,构造min+转移矩阵即可,每次转移到下一个特殊点然后暴力处 ...

随机推荐

  1. 偏差-方差分解Bias-Variance Decomposition

    转自: http://www.cnblogs.com/jmp0xf/archive/2013/05/14/Bias-Variance_Decomposition.html

  2. Office WORD如何简繁转换

    选中要转换的文字,工具-语言,中文简繁转换.

  3. [经典面试题]在O(1)时间删除链表结点

    [题目] 给定链表的头指针和一个结点指针.在O(1)时间删除该结点.链表结点的定义例如以下: struct ListNode { int        value; struct ListNode*  ...

  4. Android第一个个人APP(帐号助手)

    第一个app上线了,关于帐号保存的一个app.本地保存,无须联网. 下载地址为:http://android.myapp.com/myapp/detail.htm?apkName=com.weeky. ...

  5. Redis管理各类型存储数据命令

    >>>字符串 1 SET key value 设置指定 key 的值 2 GET key 获取指定 key 的值. 3 GETRANGE key start end 返回 key 中 ...

  6. 目前使用过的各大厂商rtsp取流的url

    目前使用过的各大厂商取流规则是在实际的工作中遇到的相关视频接入问题,通过rtsp协议接入视频数据的一些记录,其中的图片可能来源于网络,内容部分来源于网络,本人仅仅是对相关内容作了汇总. 海康RTSP取 ...

  7. Android多线程更新UI的方式

    Android下,对于耗时的操作要放到子线程中,要不然会残生ANR,本次我们就来学习一下Android多线程更新UI的方式. 首先我们来认识一下anr: anr:application not rep ...

  8. 移动端和PC端有什么区别

    1.PC考虑的是浏览器的兼容性,而移动端开发考虑的更多的是手机兼容性,因为目前不管是android手机还是ios手机,一般浏览器使用的都是webkit内核,所以说做移动端开发,更多考虑的应该是手机分辨 ...

  9. Spring Cloud 学习总结001-服务治理-Eureka

    学习参考:http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/ spring cloud由[服务注册中 ...

  10. js常用操作事件

    触发描述 方法 用法 点击 onclick="method();"   变换 onchange="testChange();"   双击 ondblclick= ...