Codefroces 223A - Bracket Sequence【栈优化】

题目大意:

有一串只含有 "("  ")"  "["  "]" 的序列,问在该序列的 左右括号能分别配对的 所有子串中的含有方括号的个数的最大值,并输出相对应的子串。

做法:

利用一个栈来维护,每次如果有能与栈顶的元素配对的右边括号时,将该元素弹出,如果此时弹出的元素代表方括号,那么记录此时出现的下标。否则将该元素压进栈。

当然我们压进栈的是当前元素的下标。当处理完整个序列之后,栈里面存的就是所有不能配对的括号所处的下标,每两个下标之间就是能配对的,符合条件的。这样,我们最后只需要比较每两个下标之间含有的能含有方括号最多即可。而计算某个片段有多少个方括号的话,我们只需要利用之前记录的方括号的下标,计算一个前缀和,就能很方便的算出来了

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 110010
using namespace std;
char a[N];
int sta[N];
int vis[N];
int ans,l,r;
int main()
{
    scanf("%s",a);
    int len=strlen(a);
    int tail=1;
    for(int i=0;i<len;i++)
    {
        if(tail==1) {
            sta[tail++]=i;
            vis[i]+=vis[i-1];
            continue;
        }
        if(a[i]=='(')
        {
            sta[tail++]=i;
        }
        if(a[i]==')')
        {
            if(a[sta[tail-1]]=='(') tail--;
            else sta[tail++]=i;
        }
        if(a[i]=='[')
        {
            sta[tail++]=i;
        }
        if(a[i]==']')
        {
            if(a[sta[tail-1]]=='[') {
                tail--;
                vis[i]=1;
            }
            else sta[tail++]=i;
        }
        vis[i]+=vis[i-1];
    }
    vis[len]+=vis[len-1];
    if(tail==1)
    {
        cout<<vis[len]-vis[0]<<endl;
        cout<<a<<endl;
        return 0;
    }
    sta[tail++]=len;
    sta[0]=0;
    for(int i=1;i<tail;i++)
    {
        if(ans<vis[sta[i]]-vis[sta[i-1]])
        {
            ans=vis[sta[i]]-vis[sta[i-1]];
            if(i-1) l=sta[i-1]+1;
            else l=0;
            r=sta[i];
        }
    }
    cout<<ans<<endl;
    for(int i=l;i<r;i++)
        cout<<a[i];
    return 0;
}
时间: 2024-08-06 06:00:40

Codefroces 223A - Bracket Sequence【栈优化】的相关文章

Codeforces 223A Bracket Sequence [栈]

给一串由(, ), [ ,]构成的字符串,求包含[最多的合法子串 很容易,先把整个字符串丢入栈里处理 栈的每一个元素存两个东西,字符,在字符串中的位置 处理方式为如果是()匹配则直接丢弃,如果是[]匹配则在这个点vis[i]++,然后求vis的前缀和 如果栈空,则说明整个串是合法的,直接输出串 否则,扫描栈中剩下的元素的位置,这几个位置把整个原串切割成几段,这几段肯定是合法的,求这几段中的含[最大的段,可以用前缀和求得,输出这段 游标,左右区间非常容易弄错.有一次手动输出了'\0',导致看起来和

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

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

cf670E Correct Bracket Sequence Editor

Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "

贪心+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

[CF442C] Artem and Array (贪心+单调栈优化)

题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边相邻的数最小的分数.问你最多得到多少分. 将高度绘图,去除V的情况. 用单调栈优化,每个元素进栈一次,出栈一次.线性时间. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

POJ 3415 Common Substrings(长度不小于k 的公共子串的个数--后缀数组+单调栈优化)

题意:给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 样例1: A="xx",B="xx",k=1,长度不小于k 的公共子串的个数是5. 样例2: A ="aababaa",B ="abaabaa",k=2,长度不小于k 的公共子串的个数是22. 思路: 如果i后缀与j后缀的LCP长度为L, 在L不小于K的情况下, 它对答案的贡献为L - K + 1. 于是我们可以将两个串连起来, 中间加个奇葩的分隔符