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. maven生命周期和依赖的范围

    转载:http://blog.csdn.net/J080624/article/details/54692444 什么是依赖? 当 A.jar 包用到了 B.jar 包时,A就对B产生了依赖: 在项目 ...

  2. 多线程调用COM组件的体会(CoInitialize)

    调用任何COM组件之前,你必须首先初始化COM套件环境,即调用CoInitialize或CoInitializeEx.COM套件环境在线程的生存周期内有效,线程退出前需要调用CoUninitializ ...

  3. 【APUE】关于信号的一些常用函数

    kill和raise函数 #include <signal.h> int kill(pid_t pid,int signo); int raise(int signo);//两个函数返回值 ...

  4. python实现接口自动化

    一.总述 Postman:功能强大,界面好看响应格式自主选择,缺点支持的协议单一且不能数据分离,比较麻烦的还有不是所有的公司都能上谷歌SoupUI:支持多协议(http\soup\rest等),能实现 ...

  5. Android耳机线控具体解释,蓝牙耳机button监听(仿酷狗线控效果)

    转载请注明出处:http://blog.csdn.net/fengyuzhengfan/article/details/46461253 当耳机的媒体按键被单击后.Android系统会发出一个广播.该 ...

  6. 发挥bat的作用

    from 转自:http://blog.csdn.net/hitlion2008/article/details/7467252 1.什么是Windows BATCH BATCH也就是批处理文件,有时 ...

  7. Windows 7旗舰版安装Visual Studio 2013 Ultimate的系统必备及注意事项

    系统必备: 1.Windows7 SP1 2.IE 10

  8. 总结文件操作函数(二)-C语言

    格式化读写: #include <stdio.h> int printf(const char *format, ...);                   //相当于fprintf( ...

  9. 异步编程错误处理 ERROR HANDLING

    Chapter 16, "Errors and Exceptions," provides detailed coverage of errors and exception ha ...

  10. XMU 1606 nc与滴水问题 【模拟】

    1606: nc与滴水问题 Time Limit: 1000 MS  Memory Limit: 64 MBSubmit: 85  Solved: 27[Submit][Status][Web Boa ...