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

题目大意:给你一串字符串,其中有'('、')'、'?'三种字符'?'可以当成'('或者')'来用,问该字符串中有多少子串符合括号匹配的规则。

解题思路:根据括号匹配原始版进行改进,设置high和low分别表示未匹配左括号数的上限和下限,当遇到'('时low++,high++,遇到')'时low--,high--,遇到'?'时由于既可以表示'('又可以表示')',所以high++,low--。

原始版括号匹配(top为未匹配左括号数):

 bool check(string s) {
int top = ;
for(int i = ; i < s.size(); ++i) {
if(s[i] == '(') top++;
else top--;
if(cnt < ) return false;
}
return cnt == ;
}

本题代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int main(){
int low,high,size;
string str;
cin>>str;
size=str.size();
int ans=;
for(int i=;i<size;i++){
low=high=;
for(int j=i;j<size;j++){
if(str[j]=='(') high++,low++;
if(str[j]==')') high--,low--;
if(str[j]=='?') high++,low--;
if(high<)
break;
if(low<) low=;
if((j-i)%&&low==)
ans++;
}
}
printf("%d\n",ans);
return ;
}

Codeforces 918C The Monster(括号匹配+思维)的更多相关文章

  1. Codeforces 918C - The Monster

    918C - The Monster 思路1: 右键在新窗口打开图片 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  2. Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)

    大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...

  3. CodeForces - 5C(思维+括号匹配)

    题意 https://vjudge.net/problem/CodeForces-5C 给出一个括号序列,求出最长合法子串和它的数量. 合法的定义:这个序列中左右括号匹配. 思路 这个题和普通的括号匹 ...

  4. 带问号的括号匹配问题918C 1153C

    cf里好像经常出 这些题,一般贪心是搞不了的.. 918C 问有多少子段[l,r]满足合法括号 先从左往右扫,如果问号+‘(' 数量 >= ')' 说明这段区间的 ) 是满足条件的 然后再从右往 ...

  5. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

  6. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

  7. CodeForces 917A The Monster 贪心+思维

    As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Chris ...

  8. 括号匹配 区间DP (经典)

    描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...

  9. YTU 3003: 括号匹配(栈和队列)

    3003: 括号匹配(栈和队列) 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 [提交][状态][讨论版] 题目描述 假设一个表达式中只允许包含三种括号:圆括号&quo ...

随机推荐

  1. 洛谷 3201 [HNOI2009]梦幻布丁 解题报告

    3201 [HNOI2009]梦幻布丁 题目描述 \(N\)个布丁摆成一行,进行\(M\)次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为\(1,2,2 ...

  2. MapReduce(四) 典型编程场景(二)

    一.MapJoin-DistributedCache 应用 1.mapreduce join 介绍 在各种实际业务场景中,按照某个关键字对两份数据进行连接是非常常见的.如果两份数据 都比较小,那么可以 ...

  3. [POI2011]SEJ-Strongbox

    题目大意: 一个有密码箱,数字是0~n-1,其中有若干个密码,密码的特点:若x是密码,y是密码,(x可以等于y)则(x+y)%n也是密码. 给一个n(<=10^14),一个k(k<=min ...

  4. 数字表格(product)

    Portal -->broken qwq Description ​  求\(\prod\limits_{i=1}^n\prod\limits_{j=1}^m f[gcd(i,j)](mod\ ...

  5. openCV实例:Canny边缘检测

    http://blog.sina.com.cn/s/blog_737adf530100z0jk.html 在第一次使用openCV程序成功对图像进行打开后,现在开始试验第二个例程试验:Canny边缘检 ...

  6. laravel添加日常备份任务

    app/Console/Command/MySqlDump.php <?php namespace App\Console\Commands; use Illuminate\Console\Co ...

  7. 对于redis框架的理解(三)

    上一篇讲完了initServer的大体流程,其中aeCreateEventLoop(),这个函数 没有详细说明,我们在这一篇里讲述Ae.h和Ae.c, 这里面的api阐述了如何创建 eventLoop ...

  8. STL源码分析-rbtree

    http://note.youdao.com/noteshare?id=4f8d16e565478b4e98bf2e56ce98a28e

  9. [DeeplearningAI笔记]序列模型2.1-2.2词嵌入word embedding

    5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.1词汇表征 Word representation 原先都是使用词汇表来表示词汇,并且使用1-hot编码的方式来表示词汇 ...

  10. form:select form:options 标签数据回显

    在jsp页面中经常会使用到 form:select form:options 遍历后台List集合生成 select 下拉选择器,但是 form:options 标签并没有提供一个可以回显数据的属性. ...