Codeforces Round #459 (Div. 2)C. The Monster
1 second
256 megabytes
standard input
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.
The first and only line of input contains string s, consisting only of characters '(',
')' and '?' (2 ≤ |s| ≤ 5000).
Print the answer to Will's puzzle in the first and only line of output.
((?))
4
??()??
7
For the first sample testcase, the pretty substrings of s are:
- "(?" which can be transformed to "()".
- "?)" which can be transformed to "()".
- "((?)" which can be transformed to "(())".
- "(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are:
- "??" which can be transformed to "()".
- "()".
- "??()" which can be transformed to "()()".
- "?()?" which can be transformed to "(())".
- "??" which can be transformed to "()".
- "()??" which can be transformed to "()()".
- "??()??" 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的更多相关文章
- Codeforces Round #459 (Div. 2)The Monster[匹配问题]
题意 给一个序列,包含(,),?,?可以被当做(或者),问你这个序列有多少合法的子序列. 分析 n^2枚举每一个子序列,暂时将每个?都当做右括号,在枚举右端点的时候同时记录两个信息:当前左括号多余多少 ...
- 【Codeforces Round #459 (Div. 2) C】The Monster
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 左括号看成1 右括号看成-1 设置l,r表示前i个数的和的上下界 遇到 左括号 l和r同时加1 遇到右括号 同时减1 遇到问号 因为 ...
- Codeforces Round #459 (Div. 2)
A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...
- Codeforces Round #459 (Div. 2):B. Radio Station
B. Radio Station time limit per test2 seconds memory limit per test256 megabytes Problem Dsecription ...
- Codeforces Round #459 Div. 1
C:显然可以设f[i][S]为当前考虑到第i位,[i,i+k)的状态为S的最小能量消耗,这样直接dp是O(nC(k,x))的.考虑矩阵快速幂,构造min+转移矩阵即可,每次转移到下一个特殊点然后暴力处 ...
随机推荐
- 获取鼠标位置的几个通用的JS函数
原文:http://www.open-open.com/code/view/1421401009218 /*两个通用函数,用于获取鼠标相对于整个页面的当前位置*/ function getX(e) { ...
- IntelliJ IDEA14.0.3+Maven+SpringMVC+Spring+Hibernate光速构建Java权限管理系统(三)
注册登录 --利用简单的编写注册登录系统来打通从前端到后台的数据传输路径. 一.建立数据库.基本表 基本环境:mysql5,7.Navicat for MySQL11.0.9企业版. 我们在本地MyS ...
- 【Nginx】http模块的数据结构
定义fttp模块方式很简单,比如:ngx_module_t ngx_http_mytest_module; 其中,ngx_module_t是一个Nginx模块的数据结构. typedef struct ...
- SqlServer 经常使用分页方法总结
SqlServer 经常使用分页方法总结 以下演示样例总结了,SqlServer数据库 经常使用分页方法,仅供学习參考 A. 使用 RowNumber 和 Between And 组合分页: /*** ...
- Android与设计模式——代理(Proxy)模式
在阎宏博士的<JAVA与模式>一书中开头是这样描写叙述代理(Proxy)模式的: 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理模式 ...
- HDU 2795 Billboard(宣传栏贴公告,线段树应用)
HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意: 要在h*w宣传栏上贴公告,每条公告的高度都是为1的,并且每条公告都要 ...
- 【转载】HTTP协议与WEB本质
当你在浏览器地址栏敲入"http://www.csdn.net/",然后猛按回车,呈现在你面前的,将是csdn的首页了(这真是废话,你会认为这是理所当然的).作为一个开发者,尤其是 ...
- leveldb学习:DBimpl
leveldb将数据库的有关操作都定义在了DB类,它负责整个系统功能组件的连接和调用.是整个系统的脊柱. level::DB是一个接口类,真正的实如今DBimpl类. 作者在文档impl.html中描 ...
- PHP中常见的header类型
<?php // 使用 mime_content_type() 查看 $mimetypes=array( 'ez' => 'application/andrew-inset', 'hqx' ...
- Windows 7旗舰版安装Visual Studio 2013 Ultimate的系统必备及注意事项
系统必备: 1.Windows7 SP1 2.IE 10