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 ...
随机推荐
- java使用io流实现图片复制
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException; public cl ...
- eclipse创建文件携带作者时间
windows–>preference Java–>Code Style–>Code Templates code–>new Java files 编辑它 ${filecomm ...
- UML-如何迭代
未完待续...
- 【lca+输入】Attack on Alpha-Zet
Attack on Alpha-Zet 题目描述 Space pirate Captain Krys has recently acquired a map of the artificial and ...
- Django+ajax 返回json数据挨个显示在页面及页面和后台相互传值
通过Ajax传到后台一个值,根据该值返回数据库表中的某一列的值,然后逐个显示到页面,并且给每个加上超链接,可以进行点击查看详细信息 1.通过Ajax传到后台一个值,红色部分为往Django后台传值,蓝 ...
- Nginx_安全1
Nginx 安全 nginx隐藏版本号 # 在Nginx的配置文件中进行修改,增加下面这个. server_tokens on; nginx对IP和目录限速 # Nginx可以通过HTTPLimitZ ...
- Sqlite教程(4) Activity
之前我们已经有了DbHelper.Data Access Object.Configuration. 那麽现在就是由Activity去创建它们,然後就可以存取Sqlite. 架构图表示了它们的关系. ...
- 基础篇十:模块介绍(--with-help_random_index_module)
配置语法: 下面开始配置: cd /etc/nginx/conf.d/default.conf
- 吴裕雄--天生自然C语言开发:数组
] = {1000.0, 2.0, 3.4, 7.0, 50.0}; ]; #include <stdio.h> int main () { ]; /* n 是一个包含 10 个整数的数组 ...
- ubuntu .bashrc文件添加jdk后无法登录的解决方案
1. 快捷键(ctl-alt-f2)进入虚拟终端 2. 执行export PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/ ...