博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 918C The Monster
阅读量:5323 次
发布时间:2019-06-14

本文共 3379 字,大约阅读时间需要 11 分钟。

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

 

 

大意:

给一个括号和问号组成的序列,问号可以视为左括号或右括号。问共有多少个合法(左右括号匹配,具体规则详见题意)的区间数(不是方案数)。

 

 

题解:

直觉是用栈,然而实际上用不到栈。

O(N^2)的复杂度是可以解决问题的。

枚举区间左端点,从每个左端点尽量向右延伸,在延伸的过程中统计合法区间数。

 

那么问题就在于如何判断当前区间是否合法:

策略:在延伸的过程中统计'('的数量,如果碰到‘?’,也记录下来,并将'?'暂时看为')',如果碰到右括号且左括号数量不够时,将一个'?'变为左括号。

当然,如果'('和'?'数量相等时碰到'?',直接看成'('。因为这种情况下,前面的'?'必须都看做')'。

维护两个变量 L和 off。L代表左括号的数量,off代表被看为')'的'?'数量。

当前序列长度为偶数时,答案+1即可。

1 /* 2 Welcome Hacking 3 Wish You High Rating 4 */ 5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 using namespace std;14 int read(){15 int xx=0,ff=1;char ch=getchar();16 while(ch>'9'||ch<'0'){ if(ch=='-')ff=-1;ch=getchar();}17 while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}18 return xx*ff;19 }20 int len,ans;21 char s[5010];22 int main(){23 //freopen("in","r",stdin);24 gets(s+1);25 len=strlen(s+1);26 for(int i=1;i
View Code

 

 

 

 

转载于:https://www.cnblogs.com/lzhAFO/p/8385922.html

你可能感兴趣的文章
js千分位处理
查看>>
Mac---------三指拖移
查看>>
字符串类型的相互转换
查看>>
HTTP状态码
查看>>
iOS如何过滤掉文本中特殊字符
查看>>
基础学习:C#中float的取值范围和精度
查看>>
javaagent 简介
查看>>
python升级安装后的yum的修复
查看>>
Vim配置Node.js开发工具
查看>>
web前端面试题2017
查看>>
ELMAH——可插拔错误日志工具
查看>>
MySQL学习笔记(四)
查看>>
【Crash Course Psychology】2. Research & Experimentation笔记
查看>>
两数和
查看>>
移动设备和SharePoint 2013 - 第3部分:推送通知
查看>>
SOPC Builder中SystemID
查看>>
MySQL数据库备份工具mysqldump的使用(转)
查看>>
NTP服务器配置
查看>>
关于 linux 的 limit 的设置
查看>>
HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
查看>>