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. windows下开发PHP扩展dll(无需Cygwin)

    windows下开发php扩展网上很多资料都说需要Cygwin,其实完全可以不必安装该东东.没错,是可以在linux下生成骨架后拷到windos下来用,但是,如果没有linux环境呢?什么,装虚拟机? ...

  2. Bound mismatch: The typae CertificateDirectory is not a valid substitute for the bounded parameter <M extends Serializable>

    这是因为架包没导对或者关联的项目不是在同一个工作空间.

  3. Firefox下td用display控制页面导致页面变形

    Firefox下table的td元素假设使用了display:'block'会使得table变形.原因是block会将对象强制作为块对象呈递,为对象之后加入新行,所以并不适合td,改成display: ...

  4. Highways POJ 2485【Prim】

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  5. poj 1426 Find The Multiple ( BFS+同余模定理)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

  6. Mac 使用smb协议连接FTPserver

    在Mac中,能够通过smb协议作为client连接到server,比如一个FTPserver,然后获取上面的共享文件. 方法: 1.在Finder菜单中点击前往 -- 连接server. 也能够Com ...

  7. 编程之美 之 让CPU占用率听你指挥

    昨天在bbs上淘到了这本编程之美.顺手刷了第一章,很有意思.第一章的要求是要控制CPU曲线,绘制出对应的形状. 拿到这个问题,我的第一反应是, 是不是有这么一个API,能在任务管理器上的对应区域直接绘 ...

  8. javascript/jquery模板引擎——Handlebars初体验

    Handlebars.js下载地址:http://handlebarsjs.com/ 最近自己在建一个站,采用完全的前后端分离的方式,现在正在做前端的部分.其中有项功能是需要ajax调用后端接口,返回 ...

  9. ffmpeg转码本地文件(一)

    ffmpeg转码本地文件(一) 实现目标:输入本地文件.实现本地文件转码,里面包括mux层转码,codec层转码,视频格式转换,音频重採样等功能,功能点请看凝视.注意:凝视非常重要. #ifndef ...

  10. 解决IOS滑动页面fixed浮动问题

    <div style="position: fixed"></div> <div style="height: 100%; overflow ...