CF - div2 - 459 - C - The Monster

西门子V90 PN伺服控制核心:FB284功能块详解与实战 在工业自动化中,伺服定位控制是运动控制的基础环节,其本质是PLC通过PROFINET协议与伺服驱动器实现高速、确定性数据交互。FB284作为西门子为V90 PN定制的标准化功能块,封装了GSD映射、状态机管理与周期性PDO同步机制,将底层寄存器操作抽象为高可靠调用接口。相比MOVE指令或MC_MoveAbsolute等通用方案,FB284显著降低时序错乱风险,提升多轴同步精度与故障响应能力。它广泛应用于包装、灌装、装配等中端产线,尤其适配S7-1200/1500平台下的快速部署与现场维护。本文聚焦V90 P 阅读详情

C. The Monster

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 “()()()”.

题目链接:http://codeforces.com/contest/918/problem/C


题意:

  • 输入一个字符串 s

问:有多少个子串是好串?

好串的定义:

  1. Empty string is a correct bracket sequence.
  2. if s is a correct bracket sequence, then (s) is also a correct bracket sequence.
  3. if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.


解题思路:见代码

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
using namespace std;
typedef long long LL;

const int maxn = 50005;
int l[maxn];//l[i]:记录的是以坐标 i 为左端点,能满足为好串,并使得串最长的【右端点坐标】 + 1 的坐标。
int r[maxn];//r[i]:记录的是以坐标 i 为右端点,能满足为好串,并使得串最长的【左端点坐标】 - 1 的坐标。
char s[maxn];

int main(){
    cin >> s + 1;
    int len = strlen(s + 1);
    for(int i = 1;i <= len;i++){//从前往后
        int cnt = 0,pos = len + 1;
        for(int j = i;j <= len;j++){
            if(s[j] == '(' || s[j] == '?') cnt++;
            else cnt--;
            if(cnt < 0){
                pos = j;
                break;
            }
        }
        l[i] = pos;
    }
    for(int i = len;i >= 1;i--){//从后往前
        int cnt = 0,pos = 0;
        for(int j = i;j >= 1;j--){
            if(s[j] == ')' || s[j] == '?') cnt++;
            else cnt--;
            if(cnt < 0){
                pos = j;
                break;
            }
        }
        r[i] = pos;
    }
    int sum = 0;
    for(int i = 1;i <= len;i++){//枚举区间(i,j)
        int a = 0,b = 0,c = 0;
        for(int j = i;j <= len;j++){
            if(l[i] <= j) break;//以坐标 i 为左端点,能满足为好串,并使得串最长的【右端点坐标】 + 1 的坐标 小于等于 j。说明以坐标 i 为左端点的好串不存在。
            if(s[j] == '(') a++;
            else if(s[j] == ')') b++;
            else if(s[j] == '?') c++;
            if((abs(a - b) + c) % 2 == 0){//abs(a - b)代表还缺 "(" 或 ")" 的个数,若缺少的能用 "?" 补全 
                if(r[j] < i) sum++;//且以坐标 i 为右端点,能满足为好串,并使得串最长的【左端点坐标】 - 1 的坐标 小于 i 的话,说明区间(i,j)是一个好串。
            }
        }
    }
    cout << sum << endl;
    return 0;
}
如果使用ssms连接SQLserver的时候出现无法连接到服务器。 如果使用ssms连接SQLserver的时候出现无法连接到服务器。 Lan 2020-03-29 22:11 329 人阅读 0 条评论 标题: 连接到服务器------------------------------无法连接到... 阅读详情

相关推荐

Logisim实战:手把手教你设计硬布线版MIPS CPU(多周期/8指令/附电路图)

本文是一份详细的Logisim实战指南,手把手教你设计一个采用硬布线控制的多周期MIPS CPU。文章深入剖析了硬布线控制与微程序控制的核心理念差异,并逐步讲解了从定义执行状态、构建状态跳转逻辑到生成控制信号、集成测试的完整流程,最终实现一个支持8条指令、可运行简单程序的处理器模型。

g9h0i1的博客 604

Div2每天两题

DIV每日一题

Funing7的博客 1133

随机森林文本分类

随机森林原理及代码实现 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录随机森林原理及代码实现前言一、集成算法是什么?二、随机森林是什么三、 数据预处理3.1 先来看一下我们的文本数据3.2 预处理3.3 统计评论情感值积极与消极的占比二、使用步骤参数介绍(4个)属性1.引入库2.参数调参方法总结 前言 提示:这里可以添加本文要记录的大概内容: 例如

m0_46579176的博客 5814

大学英语(第六册)复习(原文及全文翻译)——Unit 6 - The Monster(怪杰)

Unit 6 – The Monster This essay on a famous man, whose name is not revealed until almost the end of the piece, is a study of monstrous conceit. Filled with biographical details that keep the reader guessing to the last moment, the essay concludes with a c

预见未来to50的专栏 1万+

华为MA5620配置宽带及语音实例

华为EPON配置实例1、公共部分和宽带PPPOE部分用串口用户名密码root/mduadminMA5620E>enableMA5620E#configMA5620E(config)#MA5620E(config)#sysnamebigcow/给设备起名字/****之前为公共部一、建vlan和地址bigcow(config)#vlan100...

weixin_33862041的博客 6351

UE4逆向实战:如何快速定位UWorld和GName的5种实用技巧(含版本差异分析)

本文详细介绍了UE4逆向工程中快速定位UWorld和GName的5种实用技巧,包括特征码定位法、动态行为分析法、静态符号分析法等,并针对不同版本差异提供了适配方案。特别强调了UWorld作为游戏世界核心容器和GName作为名称管理系统的重要性,帮助开发者高效进行游戏逆向分析。

star的博客 909

信奥入门级相关的比赛(2025.01.13)

信奥入门级相关的比赛(2025.01.13)

青少年趣味编程 3924

Ansys 2022R2安装教程

Ansys 2022r2 手把手安装过程

Sim Pro的博客 3万+

代码源每日一题Div2

思路:开始思路是找规律,for遍历每个数,然后插空什么的,结果问题就是有很多情况考虑不到.最后换了dfs解法,首先判断数量最多的数的数量和其他数数量总和的关系1.如果数量最多的数的数量大于其他数数量总和+1,那么一个隔一个插空都插不了,空不够,直接判失败2.如果数量最多的数的数量等于其他数数量总和+1,这个时候就只能把最大的这个数推进用于排序的数组3.如果数量最多的数的数量小于其他数数量总和+1,这个时候就是选合适的最小的数的时候了最后把用于排序的数组遍历输出一下,结束。

sign_river的博客 841

【专栏导引贴】100+智能优化算法:从背景原理到代码实战(持续更新)

本专栏系统整理100+种现代优化算法,涵盖粒子群优化(PSO)、遗传算法(GA)、鲸鱼优化(WOA)等经典方法,以及白鲸优化(BWO)、非洲秃鹫优化(AVOA)等新兴算法。每篇详解包含生物启发原理、数学模型、代码实现及工程应用,采用统一编程风格,提供可直接运行的实例代码。专栏持续更新,旨在为研究者提供系统化的智能优化算法参考资料,实现从理论到实践的完整知识链。目前已收录23种算法,包括蚁群系统(ACS)、蝙蝠算法(BA)、天牛须搜索(BAS)等自然启发式优化方法。

qq_42212808的博客 3776

【GlobalMapper精品教程】006:Excel表格(.xls)或文本(.txt .csv)坐标文件生成矢量点

原始的坐标数据如下图所示,其中坐标为Lat经度,LNG纬度,均为十进制度的形式,一般软件都支持十进制度的格式,如果不支持,你可以尝试使用度分秒的形式,图中的DDMMSS-LAT为纬度,DDMMSS-LNG为经度。打开globalmapper软件,点击【打开数据文件】,选择坐标数据文件(案例中的数据位配套实验数据包中的data006.rar)气象台站分布点坐标.xls。选择坐标系:本案例数据采用的是经纬度,因此选择Geographic(Latitude/Longitude),基准选择WGS84,点击确定。

智慧空间实验室 2584

Codeforces Round #616 (Div. 2)(题解)

写在前面: 疫情严重,希望武汉与中国一起加油。 这次就做出两题,第三题没读完,实在是比较慢。 A. Even But Not Even time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Let’s define a number ebne...

Sqssq的博客 4470

互联网人提高效率必备软件合集

很多的时候我们解决不了问题,只是因为没有找到对的工具,实际上,很多好的工具就在那里,只是你不用而已。这点上,信息壁垒是一个很大的原因,因此我希望兄伙伴们多出去走走,不要只是局限在一个圈层;

zhonglunshun的专栏 3492

Codeforces Round #459 (Div. 2) C. The Monster

链接 就是昨晚的cf题。但是自己卡了半天,最后写了个dp也还是逃不过n^3,超时。 但是看到一个很好很好的想法,现在就想记一下而已。没有代码。 对于一个随机序列,我们设置一个l和r 干嘛的? 我们吧“?”先全部看成是“(”或“)” 然后l记录把?看成)后未配对的(的个数 然后r记录把?看成(后未配对的(的个数 来一个双重循环 ans=0; for()  l=0,r=0;

ff137774288的博客 340

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

div#div2

2013-06-04 16:01 提问者采纳 个人习惯问题,一方面便于阅读,了解id为div2的元素的隶属关系,其次也是为了避免冲突! 在定义指定id元素样式时不是很明显,但是在定义指定class元素的样式的时候就显得比较重要。 例如简单的列表,主体左侧和右侧都有,为了避免样式重叠,就需要在选择器名称前加上上级(树)的前置限定。 当然,上面的这个可以不写,根据个人的编程

paullinjie的博客 1690

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

数据结构总结

一些知识点,和多道求时间复杂度的题

weixin_46063117的博客 689

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

The Monster ( 暴力模拟)

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,...

码农的一亩三分地 377

The Monster (思维题)

The Monster 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 dir...

codeswarrior的博客 529

The Monster

 A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and Morty screa

w865629524的博客 826

Codeforces Round #459 (Div. 2)The Monster[匹配问题]

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 s

Haipai1998的博客 701

codeforces #406 A. The Monster

A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and Morty screams

yeleng的博客 338

cf 917A The Monster

一 原题 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

max_kibble的博客 550

codeforces918C 917A The Monster 【贪心 + 思维枚举】

C. The Monster time limit per test1 second memory limit per test256 megabytes As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can

nobleman 1818

CF917A: The Monster 题解

题目链接: http://codeforces.com/contest/918/problem/C 分析 一个长度为|s|的字符串是pretty的充要条件如下: 1. |s| is even. 2. 0 ≤ s[1..i].count(‘(‘) + s[1..i].count(‘?’) - s[1..i].count(‘)’) for each 1 ≤ i ≤ |s|. 3. 0 ≤

z's blog 511

CodeForces - 918C The Monster (暴力)

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 stil

LP_Cong 766

C. The Monster 括号匹配

The Monster 题目大意: 给出一个串,只包含 ( ? ) 三种符号,求出有多少个子串是完美匹配的. 比如 ( ) ? ) => ( ) ( ) 完美匹配 ( ( ) ? => ( ( ) )完美匹配 ? ? ? ? => ( ) ( ) => ( ( ) ) 算一种子串 length 分析: 这个长度和时间限制比较诡异,想不到比较好的方法,猜想

Joovo的博客 473
上一篇: ZOJ - 3769 - Diablo III (分组背包 ,dp)
下一篇: AtCoder - Petrozavodsk - Contest - 001 - D (缩点 + 贪心 + 错误总结)
dwenhu___
博客等级 码龄10年 8粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值