Codeforces Round #459 (Div. 2)The Monster[匹配问题]

题意

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

分析

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;
}

原文地址:https://www.cnblogs.com/chendl111/p/8391196.html

时间: 2024-08-03 10:30:31

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

Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以当作 ( 可 ) . 问这个字符串有多少个子串是正确的括号串. tags:好考思维,想不到.. 预处理出每个字符向左向右最多可以匹配到哪里,再 O(n*n) 枚举所有区间,看是否符合条件. // C #include<bits/stdc++.h> using namespace std; #pra

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

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 左括号看成1 右括号看成-1 设置l,r表示前i个数的和的上下界 遇到 左括号 l和r同时加1 遇到右括号 同时减1 遇到问号 因为问号可以为1或-1所以l减1而r加1 如果在某个时刻r小于0了 就说明再也不能继续了 因为说明左括号和问号比右括号来得少了 如果l<0则把l置为0,因为不允许和出现<0的情况 否则如果l等于0就说明有一种方法能让前i个数的和为0 当然当前枚举的序列长度要为偶数 [代码] #include <

Codeforces Round #459 (Div. 2)题解

补题 codeforces 918C 题意 给定一个含有通配符?和()的字符串,问有多少子串是括号匹配的 解题思路 首先考虑不用栈求括号匹配的方法: bool solve(char* s) { int top=0; for (int i=0;i<strlen(s);i++) { if(s[i]=='(') top++; else top--; if(top<0) return false; } return top==0; } 由于在使用栈的括号匹配算法中,栈内是不存在)的,所以不难证明这个算

【Codeforces Round #459 (Div. 2) B】 Radio Station

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map模拟一下映射就好了. [代码] #include <bits/stdc++.h> using namespace std; int n,m; map<string,string> dic; int main(){ #ifdef LOCAL_DEFINE freopen("rush_in.txt", "r", stdin); #endif ios::sync_with_

Codeforces Round #459 (Div. 2) AB

A. Eleven Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. Eleven wants her name to have exactly n characters. Her friend suggested that her name should only consist of u

Codeforces Round #261 (Div. 2) 459B. Pashmak and Flowers(数学题,组合)

题目链接:http://codeforces.com/problemset/problem/459/B B. Pashmak and Flowers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak decided to give Parmida a pair of flowers from the garden.

Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak has fallen in love with an attractive girl called Parmida s

Codeforces Round #129 (Div. 1)E. Little Elephant and Strings

Codeforces Round #129 (Div. 1)E. Little Elephant and Strings 题意:给出n个字符串,问每个字符串有多少个子串(不同位置,相同的子串视作不同)至少出现在这n个字符串中的k个当中. 解法:这题学到了一个SAM的新技能,对于这多个串,建SAM的时候,不是把它们连在一起,建立SAM,而是先给它们建立Trie树,然后广搜这棵Trie树,对于Trie树上的V节点,在建SAM的时候,它应该接在Trie树上他的父亲节点后面,我们用TtoM[U]表示Tr

Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Parmida is a clever girl and she wants to participate in O