题意

给一个序列,包含(,),?,?可以被当做(或者),问你这个序列有多少合法的子序列。

分析

n^2枚举每一个子序列,暂时将每个?都当做右括号,在枚举右端点的时候同时记录两个信息:当前左括号多余多少个(top),已经将多少个?变成了右括号(cnt)。

如果当前是(,top++.

如果当前是),top--.

如果当前是?,top--,cnt++;

如果top<0,我们需要判断一下当前还有没有之前被当做右括号的?,如果有的话,我们将其变为左括号,如果没有的话,意味着可以跳出循环了,之后都不会再合法了。

如果任何时刻top==0,那么意味着当前区间合法,ans++。

巧妙地贪心

代码

#include <bits/stdc++.h>
using namespace std; #define ll long long char s[5050]; int main(int argc, char const *argv[])
{
scanf("%s",s);
int ans=0;
for(int i=0;s[i]!='\0';++i)
{
int left=0,cnt=0;
for(int j=i;s[j]!='\0';++j)
{
if(s[j]=='(') left++;
if(s[j]==')') left--;
if(s[j]=='?') left--,cnt++;
if(left<0&&cnt) left+=2,cnt--;
if(left==0) ans++;
if(left<0&&!cnt) break;
}
}
printf("%d\n",ans );
return 0;
}

Codeforces Round #459 (Div. 2)The Monster[匹配问题]的更多相关文章

  1. Codeforces Round #459 (Div. 2)C. The Monster

    C. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. Codeforces Round #459 (Div. 2) C题【思维好题--括号匹配问题】

    题意:给出一个串,只包含 ( ? ) 三种符号,求出有多少个子串是完美匹配的. ( ) ? ) => ( ) ( ) 完美匹配( ( ) ? => ( ( ) )完美匹配? ? ? ? = ...

  3. 【Codeforces Round #459 (Div. 2) C】The Monster

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 左括号看成1 右括号看成-1 设置l,r表示前i个数的和的上下界 遇到 左括号 l和r同时加1 遇到右括号 同时减1 遇到问号 因为 ...

  4. Codeforces Round #459 (Div. 2)

    A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  5. Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理

    https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...

  6. Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈

    D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  7. Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)

    D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...

  8. Codeforces Round #459 (Div. 2):B. Radio Station

    B. Radio Station time limit per test2 seconds memory limit per test256 megabytes Problem Dsecription ...

  9. Codeforces Round #406 (Div. 2) A MONSTER

    A. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. python网络爬虫之如何识别验证码

    有些网站的登录方式是验证码登录的方式,比如今天我们要测试的网站专利检索及分析. http://www.pss-system.gov.cn/sipopublicsearch/portal/uilogin ...

  2. ABAP screen

    Instance One : SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-100. SELECTION-SCREEN BEGIN ...

  3. Python实现简易HTTP服务器

    一.Python3 搭建简易HTTP服务器 python -m http.server 浏览器访问:http://localhost:8000 Python3 cgiserver python -m  ...

  4. opencv操作相机相关函数

    1.基本操作 capture = cv2.VideoCapture(0) ret, image = capture.read() cv2.imwrite("photo.jpg", ...

  5. /etc/init.d/nginx

    #! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon ...

  6. SDUT OJ 河床

    河床 Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 地理学家们经常要对一段河流进行测量分析.他们从上游开始向下游方向等距离地选择 ...

  7. 浅析android中的依赖注入

    这几年针对Android推出了不少View注入框架,例如ButterKnife.我们首先来了解一下使用这些框架有什么好处,其实好处很明显:它可以减少大量的findViewById以及setOnClic ...

  8. BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...

  9. linux命令学习笔记(33):df 命令

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了 多少空间,目前还剩下多少空间等信息. .命令格式: df [选项] [文件] .命令 ...

  10. JSP常见知识点

    false 7.8 磅 0 2 false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTa ...