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+转移矩阵即可,每次转移到下一个特殊点然后暴力处 ...
随机推荐
- android手机rootROM下载地址
https://download.mokeedev.com/ https://download.lineageos.org/
- java 返回json数据
Student st1 = new Student(1, "dg", 18, new Date()); Student st2 = new Student(2 ...
- db2安装配置备份还原
环境 cenos 7.0 db2版本 db2_v101_linuxx64_expc.tar 安装db2 解压db2 tar zxvf db2_v101_linuxx64_expc.tar cd exp ...
- 常见Python运行时错误
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在 ...
- JVM原理及内存溢出
JVM原理及内存溢出
- Android KK后为何工厂模式下无法adb 无法重新启动机器 ?
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 一颗ARM架构芯片的软硬件构成
硬件和软件是一颗芯片系统互相依存的两大部分.本文总结了一颗芯片的软硬件组成.作为对芯片的入门级概括吧. (一)硬件 主控CPU:运算和控制核心.基带芯片基本构架採用微处理器+数字信号处理器(DSP)的 ...
- 机器学习笔记之PCA-SIFT总结
不多说,直接上干货! PCA-SIFT算法在描述子构建上作了创新,主要是 将统计学中的主成分分析(PCA)应用于对描述子向量的降维,以提高匹配效率 . PCA 的原理是:一般有效信号的方差大,噪声的方 ...
- HDFS集中式缓存管理(Centralized Cache Management)
Hadoop从2.3.0版本号開始支持HDFS缓存机制,HDFS同意用户将一部分文件夹或文件缓存在HDFS其中.NameNode会通知拥有相应块的DataNodes将其缓存在DataNode的内存其中 ...
- 2016/05/13 Thinkphp 3.2.2 ①数据添加 ②收集表单数据入库操作 ③数据修改操作
①数据查询 add() 该方法返回被添加的新记录的主键id值 两种方式实现数据添加 数组方式数据添加 $goods = D(“Goods”); $arr = array(‘goods_name’=&g ...