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. PAT甲级——1153.Decode Registration Card of PAT(25分)

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  2. [mark]C# 异常处理

    https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/exceptions/index

  3. Ubuntu源码编译安装tensorflow

    ubuntu14 cuda9.0_384.81 驱动版本384.90  cudnn7.2 tensorflow1.8 https://blog.csdn.net/pkokocl/article/det ...

  4. JS导出、导入EXCEL(案例)

    插件下载地址:http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js 1.导出excel <!DOCTYPE html> <html> ...

  5. c指针(2)

    #include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct LNode { cha ...

  6. 利用GetCharWidth32获取字符串宽度

    /////////////////////////////////////////////////////////////// // 04FirstWindow.cpp文件 #include < ...

  7. C/S 和 B/S架构

    C/S 和 B/S架构 一.单机架构 应用领域: 植物大战僵尸 office 二.C/S架构 [ 应用领域: QQ 大型网络游戏 计算机发展初期用户去取数据,直接就去主机拿,从这里开始就分出了客户端和 ...

  8. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  9. Django框架(六):模型(二) 字段查询、查询集

    1. 字段查询 通过模型类.objects属性可以调用如下函数,实现对模型类对应的数据表的查询. 函数名 功能 返回值 说明 get 返回表中满足条件的一条且只能有一条数据. 返回值是一个模型类对象. ...

  10. 17.3.13---sys.argv[]用法

    1------sys.argv[]是用来获取命令行参数, sys.argv[0]表示代码本身文件路径,因此要从第二个即sys.argv[1]开始去参数 例如创建一个文件: import sys pri ...