题目链接: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. 【JQuery】数据

    一.前言        接着前一章的内容,继续本章的学习 二.内容 queue 显示或操作在匹配元素上执行的函数队列 .queue(queueName) 操作在匹配元素上执行的函数队列 .queue( ...

  2. Codeforces Round #353 (Div. 2) A

    弱到只会写div2的A题... 题面: A. Infinite Sequence time limit per test 1 second memory limit per test 256 mega ...

  3. 51nod 1952 栈(单调队列)

    用deque实时维护栈的情况. 数加入栈顶部,删掉栈顶部的数,相当于加入一个数,删掉最早出现的数,每次求最大值,这个直接记录一下就好了. 数加入栈底部,删掉栈顶部的数,相当于加入一个数,删掉最晚出现的 ...

  4. oracle 数据库记录

    /*----------------------------------------------------------------------------*/ 问题1[--------] Selec ...

  5. NOIP2017

    NOIP2017游记 记得开始学OI是今年的6月,那时候纯粹是抱着好玩的心态来学的,但是渐渐地,我发现我好像喜欢上了OI,喜欢敲键盘时的声音,喜欢手指触碰键盘时的手感,喜欢这个奥赛班与其他科目学习气氛 ...

  6. ftp server安装与配置

    http://note.youdao.com/noteshare?id=905513cfcdba7d6a8d2fbdd0874a6259

  7. 给上传文件的input控件“美容”

    在工作中经常会遇到form表单这种东西.然而表单的其他input控件样式还是很好改变的.但是,唯独input类型是file的文件上传控件可能就没那么好打扮的漂亮. demo: html代码 <b ...

  8. eclipse/myeclipse添加插件3种方式

    个人比较偏爱links的方式,以下方式eclipse/myeclipse均适合 1.links方式 在eclipse目录先新建links目录,新建一个xx.link(例如:android.link) ...

  9. [USACO14MAR] Counting Friends

    题目描述 Farmer John's N cows (2 <= N <= 500) have joined the social network "MooBook". ...

  10. Eng1—English daily notes

    English daily notes 2015年 4月 Phrases 1. As a side note #作为附注,顺便说句题外话,和by the way意思相近,例句: @1:As a sid ...