E. Almost Regular Bracket Sequence

题目链接:http://codeforces.com/contest/1095/problem/E



解题心得:  

  刚开始拿到这个题的时候还真的没什么思路,后来仔细想想还是比较简单的。首先题目要求翻转一个括号就要达到符合括号的匹配规则,那么在匹配完符合条件的括号之后有多出的两个括号向左或者向右。

  其次从左边开始)不能比(多出两个以上,不然无法通过翻转一个得到符合规范的括号列,当刚好多出一个的时候这个括号必须翻转。


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+100;

int cnt[maxn], n;
char s[maxn];

int main() {
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    scanf("%d", &n);
    scanf("%s", s);
    int x = 0;
    for(int i=0;i<n;i++) {
        if(s[i] == ‘(‘) x++;
        else x--;
        cnt[i] = x;
    }

    for(int i=n-2;i>=0;i--)
        cnt[i] = min(cnt[i+1], cnt[i]);
    int ans = 0, b = 0;
    for(int i=0;i<n;i++) {
        if(s[i] == ‘(‘) {
            if(cnt[i] >= 2 && x == 2) ans++;
            b++;
        } else {
            if(cnt[i] >= -2 && x == -2) ans++;
            b--;
        }
        if(b < 0)
            break;
    }
    printf("%d", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/GoldenFingers/p/10200464.html

时间: 2024-10-09 13:43:43

E. Almost Regular Bracket Sequence的相关文章

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <

D - Replace To Make Regular Bracket Sequence

You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bra

Codeforces 1132A. Regular Bracket Sequence

原题链接:Codeforces 1132A. Regular Bracket Sequence 题目大意:你有\({cnt}_1,{cnt}_2,{cnt}_3,{cnt}_4\)个"((","()",")(","))",问能否将这些字符串组成一个合法的括号序列. 题解:这一道题,很明显的\({cnt}_2\)是不需要管的,对于第三种情况,它并不改变左右括号的数量差,只有第一.四情况改变,那么,很明显\({cnt}_1={cn

CodeForces 5C Longest Regular Bracket Sequence

题意:给定一串括号,求最长的规则('(())'或‘(()())’)的字串及最长字串的个数: 思路:使用栈保存左括号,与最近的右括号匹配,使用递推推出每个位置最长字串长度: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<stack> using namespace std; stack<int> t; int n,m,len

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

经过了一个多月的时间,今天终于可以回到正轨了,继续开始刷CF. 题目大意: 给出一个只有括号的字符串,求最长"匹配"子串的长度和数量. 解题思路: 设置数组记录匹配括号段的开头. 下面是代码: #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include &l

CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】

[链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈.如果遇上左括号,就加进栈里.如果遇上右括号,就判断栈里的左括号是否和它匹配,不匹配就加一.不论匹不匹配,判断后都要让左括号出栈. 如果最后栈不为空,或者栈在循环结束前就为空,那么不论怎么改变,左右括号都不可能刚好匹配. [代码]: #include<cstdio> #include<cst

Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号个数比右括号多 2 2)在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数 3)在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数减去前缀右括号个数大于2 (这里这么想,把左变成右,左-1,右+1) 右括号也是这样 代码: #include<bits/stdc++.h> usi

cf3D Least Cost Bracket Sequence

This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and