题目链接: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. spark(三)spark sql

    一.DataFrame 1.DataFrame是组织成命名列的数据的分布式集合,类似于关系型数据库的一张表,如果没有列名就等于RDD,如果有列名,就是DataFrames DataFrames可以从各 ...

  2. 【CF Gym100228】Graph of Inversions

    Portal --> qwq(貌似是CodeForces Gym 100228 (ECNA2003) - I) Description 对于长度为 \(n\) 的序列 \(A\) ,定义其逆序图 ...

  3. mesos+marathon+zookeeper+docker

    http://mesosphere.com/docs/mesosphere/getting-started/single-node-install/ mesos-master --zk=zk://lo ...

  4. 题解【bzoj2038 [2009国家集训队]小Z的袜子(hose)】

    Description \(m\) 个询问,每次给出一个区间,求从这个区间中取出两个数使得它们同色的概率. \(n,m,a_i \leq 50000\) Solution 莫队模板题 最后的概率是 选 ...

  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. linux中使用随机数

    (1)单纯使用rand重复调用n次,就会得到一个0-RAND_MAX之间的伪随机数,如果需要调整范围,可以得到随机数序列后再进行计算.(2)单纯使用rand来得到伪随机数序列有缺陷,每次执行程序得到的 ...

  7. 深度学习voc数据集图片resize

    本人新写的3个pyhton脚本. (1)单张图片的resize: # coding = utf-8 import Image def convert(width,height): im = Image ...

  8. MingW和MSVC默认的编码方式不一样

    同一份源代码,源文件编码格式为UTF-8: string tmp = "我"; ;i<tmp.size();++i) { printf("%0x ",tm ...

  9. 最短路+找规律 Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest L. Right Build

    题目链接:http://codeforces.com/gym/101149/problem/L 题目大意:有n个点(其实是n+1个点,因为编号是0~n),m条有向边.起点是0,到a和b两个节点,所经过 ...

  10. IFeatureCursorProxy.flush AutomationException: 0x80041538

    添加面的时候碰到的一个问题,有些数据没问题,有些报错,后来请教一位同事说有可能是经纬度字段的数据精度问题,因为投影坐标系统不同,支持的经纬度经度不同,后来转换投影坐标系统后果然解决问题了,我一开始也怀 ...