题目链接: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. 【BZOJ2339】卡农(递推,容斥)

    [BZOJ2339]卡农(递推,容斥) 题面 BZOJ 题解 先简化一下题意: 在\([1,2^n-1]\)中选择不重复的\(m\)个数,使得他们异或和为\(0\)的方案数. 我们设\(f[i]\)表 ...

  2. oracle的sign()函数

    sign函数 比较大小函数 sign 函数语法:sign(n) 函数说明:取数字n的符号,大于0返回1, 小于0返回-1, 等于0返回0 示例1: ),),) from dual; ) ) ) ——— ...

  3. FindBugs错误修改指南 【转】

    FindBugs错误修改指南 1. EC_UNRELATED_TYPES Bug: Call to equals() comparing different types Pattern id: EC_ ...

  4. Codeforces 311.E Biologist

    E. Biologist time limit per test 1.5 seconds memory limit per test 256 megabytes input standard inpu ...

  5. bzoj 4347 [POI2016]Nim z utrudnieniem DP

    4347: [POI2016]Nim z utrudnieniem Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 733  Solved: 281[Su ...

  6. 第4章-Vue.js 交互及实例的生命周期

    一.学习目标 了解实例生命周期的过程 理解钩子函数的作用 掌握Vue.js过滤器的使用方法 (重点) 能够使用网络请求进行前后端交互 (重点.难点) 二.交互的基本概念 2.1.前端和后端的概念 说明 ...

  7. [吴恩达机器学习笔记]14降维3-4PCA算法原理

    14.降维 觉得有用的话,欢迎一起讨论相互学习~Follow Me 14.3主成分分析原理Proncipal Component Analysis Problem Formulation 主成分分析( ...

  8. svnserver配置详解

    svnserve是SVN自带的一个轻型服务器,客户端通过使用以svn://或svn+ssh://为前缀的URL来访问svnserve服务器,实现远程访问SVN版本库. svnserve可以通过配置文件 ...

  9. web启动@Autowired不能自动注入

    使用struts2,下面为action代码 Java代码 package com.edar.web.platform; import org.apache.struts2.convention.ann ...

  10. 782E. Underground Lab DFS 好题

    Link 题意:给出一个图,有n个点,m条边,k个人,每个人至多只能走$\lceil\frac{2n}{k}\rceil$步,输出可行的方案即输出每个人所走的步数和所走点 思路: 由于保证给出的是连通 ...