题目链接: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. 【WPF】PopupColorEdit 的使用

    一.前言        PopupColorEdit 是 dev中一个常用的调色盘控件,它的Color属性返回的是一个System.Windows.Media.Color对象,而不是System.Dr ...

  2. 【字符串】manacher算法

    Definition 定义一个回文串为从字符串两侧向中心扫描时,左右指针指向得字符始终相同的字符串. 使用manacher算法可以在线性时间内求解出一个字符串的最长回文子串. Solution 考虑回 ...

  3. Centos 下 error while loading shared libraries: libopencv_core.so.3.0

    error while loading shared libraries: libopencv_core.so.3.0 Check if those libraries are present in ...

  4. 使用 ant 构建的一个例子

    在项目根目录下新建一个 build.xml 文件,内容如下: <?xml version="1.0"?> <project name="javatest ...

  5. Linux I/O缓冲

    1:两类I/O函数的缓冲机制 1.1 系统调用(System call) 这类代表就是read/write等系统函数,它们是不带缓冲的,这里的缓冲指的是进程缓冲,在内核到磁盘之间还是有内核缓冲的. 1 ...

  6. golang设置代理

    http://note.youdao.com/noteshare?id=a8df0ec2d623f282a782dbe937bdae9f

  7. Linux iptables:场景实战一

    <Linux iptables:规则原理和基础>和<Linux iptables:规则组成>介绍了iptables的基础及iptables规则的组成,本篇通过实际操作进行ipt ...

  8. count distinct

    SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders

  9. php数组定义

    $arr = array( ,), //是’0' ,不是[‘0’] ,), ,), ,), ); $arr = [ '0' => array(2,3), //是’0' ,不是[‘0’] '1' ...

  10. vue 和react

    React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供了响应式 (Reactive) 和组件化 (Composable) 的视图组件. 将注意力集中保持在核心库,而将其 ...