C. The Monster
time limit per test

1 second

memory limit per test

256 megabytes

 

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

题意:给你一个由'('、')'和'?'组成的字符串s,问你s有多少个子串是好串(可以通过将'?'转化成'('或者')';  好串的定义:形如()、(());

分析:思维题,每次都进行一下判断: l代表左括号数目,r代表问号数目,ans代表符合条件的串个数

① 若s[i]=='('  l++;

② 若s[i]==')'  l--;

③ 若s[i]=='?'  r++,l--;   (先将'?'看成右括号,同时记录'?'的数目)

④ 如果l==0  匹配成功,ans++;

⑤ 如果l<0&&r>0  l+=2,r--;  (右括号多于左括号,同时又有问号,说明右括号多于左括号的有部分原因是?造成的,所以我们把?变成左括号)

⑥ 如果l<0&&r==0  结束循环  (右括号多于左括号,不是因为?的原因,无法再形成好串)

AC代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 5010
using namespace std;
char s[N];
int main()
{
cin>>s;
int len=strlen(s);
int ans=;
for (int i=;i<len;i++)
{
int l=,r=;
for (int j=i;j<len;j++)
{
if (s[j]=='(') l++;
else if (s[j]==')') l--;
else if (s[j]=='?') l--,r++;
if (!l) ans++;
else if(l<&&r>) l+=,r--;
else if (l<&&!r) break;
}
}
cout << ans << endl;
return ;
}

The Monster(Codeforce-C-思维题)的更多相关文章

  1. Two progressions CodeForce 125D 思维题

    An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...

  2. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  3. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  4. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  5. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  6. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  7. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  8. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  9. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

随机推荐

  1. [原]你知道怎么使用DebugView查看内核调试信息吗?

    原总结注册表sysinternalsdebugviewprocess explorerprocess monitor 简介 DebugView是sysinternals工具集中的一款用来查看调试信息的 ...

  2. Servlet&JSP复习笔记 03

    1.Servlet的声明周期 容器如何创建Servlet对象,如何为Servlet对象分配资源,如何调用Servlet对象的方法来处理请求,以及如何销毁Servlet对象的过程. a.实例化 容器调用 ...

  3. 卷积神经网络---padding、 pool、 Activation layer

    #coding:utf-8 import tensorflow as tf tf.reset_default_graph() image = tf.random_normal([1, 112, 96, ...

  4. Maven--远程仓库的认证

    大部分远程仓库无须认证就可以访问,但有时候出于安全方面的考虑,我们需要提供认证信息才能访问一些远程仓库. 配置认证信息和配置仓库信息不同,仓库信息可以直接配置在 POM 文件中,但是认证信息必须配置在 ...

  5. Spring AOP中使用args表达式访问目标方法的参数

    Spring AOP 的使用过程理解 首先,aop的使用场景介绍: 1.处理一些通用的非功能性的需求,不影响业务流程,比如说打印日志.性能统计.推送消息等: 2.aop无法拦截static.final ...

  6. VMware12 + Ubuntu16.04 虚拟磁盘扩容

    转载自:https://blog.csdn.net/Timsley/article/details/50742755 今天用虚拟机的时候,发现虚拟机快满了,提示磁盘空间小,不得不扩充虚拟机空间.经过百 ...

  7. CodeForces 1005D Polycarp and Div 3(思维、贪心、dp)

    http://codeforces.com/problemset/problem/1005/D  题意: 给一个仅包含数字的字符串,将字符串分割成多个片段(无前导0),求这些片段里最多有多少是3的倍数 ...

  8. python 3.6

    安装了最新版anaconda3-4.3 发现jupyter-notebook 少了一些东西.需要手工安装 https://github.com/Anaconda-Platform/nbpresent

  9. js获取前n天或后n天的天数

    1获取n天后的某天的日期 <!DOCTYPE html> js获取前n天或者后n天的天数 </style> <body > <h1 id="date ...

  10. PAT甲级——1035 Password (20分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...