The Monster(Codeforce-C-思维题)
1 second
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.
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 "()()()".
题意:给你一个由'('、')'和'?'组成的字符串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-思维题)的更多相关文章
- Two progressions CodeForce 125D 思维题
An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- 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 ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- 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 ...
随机推荐
- keyword排序-Es问题
问题:mapping索引registerordercount字段设置为keyword,但是在进行倒序排的视乎发现,没有按预期排序. keyword类型: "registerordercoun ...
- Python模块——json
简介 json全名是JavaScript Object Notation(即:Javascript对象标记).它是JavaScript的子集,JSON是轻量级的文本数据交换格式.前端和后端进行数据交互 ...
- js 经纬度计算直线距离
function getRad(d) { var PI = Math.PI; return d * PI / 180.0; } function getDistance(lat1, lng1, lat ...
- Linux轻量级自动化运维工具— Ansible
Ansible 是什么 ? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配 ...
- 学习spring第四天
Spring第四天讲义 今日内容 Spring的事务管理 Spring和MyBatis框架的集成 1. Spring的事务管理 1.1. 事务是什么? 在操作数据库时(增删改),如果同时操作多次数据, ...
- day57-mysql-五种约束和sql语句逻辑执行顺序
二.sql语句逻辑执行顺序 () SELECT () DISTINCT <select_list> 去重复 () FROM <left_table> () <join_t ...
- [原]调试实战——使用windbg调试崩溃在ComFriendlyWaitMtaThreadProc
原调试debugwindbgcrash崩溃COM 前言 这是几年前在项目中遇到的一个崩溃问题,崩溃在了ComFriendlyWaitMtaThreadProc()里,没有源码.耗费了我很大精力,最终通 ...
- windows系统安装msi文件总提示2502、2503的错误
首先: 1.按WIN+R,在运行框中输入“gpedit.msc” 确认:2.打开本地策略组编辑器后依次展开 :“计算机配置”->“管理模板”->“windows组件”->“windo ...
- [LC] 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 21)PHP,杨辉三角
代码展示: $n=; ;$i<=$n;$i++){ ;$k<=$i;$k++){ ||$k==$i){ $arr[$i][$k]=; }else{ ){ $arr[$i][$k] = $a ...