【#138 div 1 A. Bracket Sequence】

【原题】

A. Bracket Sequence
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A bracket sequence is a string, containing only characters "(", ")", "[" and "]".

A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()[]", "([])" are correct (the resulting expressions are: "(1)+[1]", "([1+1]+1)"), and "](" and "[" are not. The empty string is a correct bracket sequence by definition.

A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is the string slsl + 1... sr. The empty string is a substring of any string by definition.

You are given a bracket sequence, not necessarily correct. Find its substring which is a correct bracket sequence and contains as many opening square brackets «[» as possible.

Input

The first and the only line contains the bracket sequence as a string, consisting only of characters "(", ")", "[" and "]". It is guaranteed that the string is non-empty and its length doesn't exceed 105 characters.

Output

In the first line print a single integer — the number of brackets «[» in the required bracket sequence. In the second line print the optimal sequence. If there are more than one optimal solutions print any of them.

Sample test(s)
input
([])
output
1
([])
input
(((
output
0

【题意】给定长度为N的小、中括号序列,求一个符合括号匹配的子串,使得中括号最多。

【分析】开始想的有点麻烦。感觉是O(N)的扫过去,遇到“)”和“]”注意判无解(如果无解前面全部舍弃掉),然后中括号匹配是这样的——判断这个]和与之匹配的[的“前缀左小括号个数”(显然“(“是会被“)”消掉的)。但是叉点重重。比如[][],第二组中括号要和第一组发生关系,因此还要用一个类似并查集的东西维护。写起来超级麻烦。

【题解】网上看的思路真是超级清晰。我们用O(N)的方法算出每个点最多能拓展到哪里。

倒着扫,用P[i]表示第i个点向右最多匹配几个字符(包括自己)。

比如当前是i,那么我们知道i+1~i+P[i+1]已经是i+1最大合法状态了。

设next=i+P[i+1]+1,那么判断一下s[i]和s[next]是否匹配。

如果匹配,就能使i~next全部匹配。这是还要判断一下:next+1后面能否继续匹配呢?

于是P[i]=next-i+1+P[next+1],即把next+1的匹配项也加进去。(就想[][]的形态)

注意,不需要加next+1+P[next+1]的P,因为这已经算在P[next+1]上了- -。

【代码】

#include<cstdio>
#include<cstring>
#define N 100005
using namespace std;
char s[N];int num[N],P[N],ans,i,x,y,L,next;
int main()
{
scanf("%s",s+);L=strlen(s+);
for (i=;i<=L;i++)
num[i]+=num[i-]+(s[i]=='[');
P[L]=;x=;y=;
for (i=L-;i;i--)
{
if (s[i]==')'||s[i]==']') continue;
next=i++P[i+];
if (s[i]=='('&&s[next]==')'||s[i]=='['&&s[next]==']')
P[i]=next-i++P[next+];
}
for (i=;i<=L;i++)
if (num[i+P[i]-]-num[i-]>ans)
ans=num[i+P[i]-]-num[i-],x=i,y=i+P[i]-;
printf("%d\n",ans);
for (i=x;i<=y;i++) printf("%c",s[i]);
return ;
}

CF#138 div 1 A. Bracket Sequence的更多相关文章

  1. CF思维联系–CodeForces -224C - Bracket Sequence

    ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...

  2. 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)

    题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...

  3. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  4. 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 o ...

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

    传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...

  6. CF1095E Almost Regular Bracket Sequence

    题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...

  7. cf3D Least Cost Bracket Sequence

    This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if b ...

  8. cf670E Correct Bracket Sequence Editor

    Recently Polycarp started to develop a text editor that works only with correct bracket sequences (a ...

  9. UESTC 1546 Bracket Sequence

                                        Bracket Sequence Time Limit: 3000MS   Memory Limit: 65536KB   64 ...

随机推荐

  1. 报表软件JS开发引用HTML DOM的location和document对象

    上一次提到,在报表软件FineReport的JavaScript开发中,可以访问并处理的HTML DOM对象有windows.location.document三种.这次就继续介绍后两种,locati ...

  2. 轻松搞懂WebService工作原理

    用更简单的方式给大家谈谈WebService,让你更快更容易理解,希望对初学者有所帮助. WebService是基于网络的.分布式的模块化组件. 我们直接来看WebService的一个简易工作流程: ...

  3. Maven配置详见

    CSDN 2016博客之星评选结果公布    [系列直播]零基础学习微信小程序!      "我的2016"主题征文活动   博客的神秘功能 maven 配置详解 标签: mave ...

  4. C++的vector对象

    C++的vector使用 标签(空格分隔): C++ 标准库类型vector表示对象的集合,其中所有对象的类型都相同.集合中的每个对象都有一个与之对应的索引,索引用于访问对象,因为vector容纳着其 ...

  5. python 类属性与方法

    Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...

  6. bootstrap的介绍 和使用

    开发中文网: v3.bootcss.com   前端?框架? 用户 -->  前端应用程序 --->后端应用程序 --->后端服务器. 框架:Library 或者lib,都指得是框架 ...

  7. ZooKeeper 笔记(5) ACL(Access Control List)访问控制列表

    zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...

  8. JS组件系列——Bootstrap 树控件使用经验分享

    前言:很多时候我们在项目中需要用到树,有些树仅仅是展示层级关系,有些树是为了展示和编辑层级关系,还有些树是为了选中项然后其他地方调用选中项.不管怎么样,树控件都是很多项目里面不可或缺的组件之一.今天, ...

  9. html 元素分类

    在讲解CSS布局之前,我们需要提前知道一些知识,在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div> ...

  10. 剑指offer 面试题64 数据流的中位数

    struct cmp { bool operator()(double a, double b) { return a > b; } }; class Solution { public: vo ...