CF#138 div 1 A. Bracket Sequence
【#138 div 1 A. Bracket Sequence】
【原题】
2 seconds
256 megabytes
standard input
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.
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.
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.
([])
1
([])
(((
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的更多相关文章
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
- 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- 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 ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)
传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...
- CF1095E Almost Regular Bracket Sequence
题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...
- cf3D Least Cost Bracket Sequence
This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if b ...
- cf670E Correct Bracket Sequence Editor
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (a ...
- UESTC 1546 Bracket Sequence
Bracket Sequence Time Limit: 3000MS Memory Limit: 65536KB 64 ...
随机推荐
- Flask入门1-HelloWorld
Flask是基于Python的轻量级Web开发框架,本文简述其构建web用用的基本步骤,以下内容默认开发环境为Ubuntu14.04. 本文参考Flask官方建教程翻译并整理:http://flask ...
- Winform listview控件、 容器控件
1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设置行和列之间是否显示网格 ...
- 6.bootstrap练习笔记-缩略图和list-group
bootstrap练习笔记-缩略图 1.其实缩略图很简单,只要按照固定的格式来设计 div.container 总容器 在宽度为1200px以上 div.row 一行内容 div.col-lg-3. ...
- [No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- Apache Shiro系列一,概述 —— 初识
一.什么是Shiro Apache Shiro是一个强大.灵活.开源的安全框架,它支持用户认证.权限控制.企业会话管理以及加密等. Apache Shiro的第一个也是最重要的一个目标就是易于使用和理 ...
- 【swift学习笔记】一.页面转跳的条件判断和传值
在转跳页面之前我们可能会判断一下是否有输入正确的数据,如下图所示,如果没有数据输入则弹出提示,不进行转跳. 一:页面转跳 1.第一个页面的上放上一个Label一个textfield和一个按钮 2.在第 ...
- 如何设置ASP.NET页面的运行超时时间
全局超时时间 服务器上如果有多个网站,希望统一设置一下超时时间,则需要设置 Machine.config 文件中的 ExecutionTimeout 属性值.Machine.config 文件位于 % ...
- JavaScript模板引擎artTemplate.js——template.compile()方法
template.compile(source, options) source:必传,渲染模板的内容. options:可选,通常不传.(其实是我还没研究明白) return:一个渲染函数. 示例如 ...
- 51Nod--1015 水仙花数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1015 1015 水仙花数 基准时间限制:1 秒 空间 ...
- sql where and or优先级 待验证
where 后面如果有and,or的条件,则or自动会把左右的查询条件分开,即先执行and,再执行or.原因就是:and的执行优先级最高! 关系型运算符优先级高到低为:not and or 问题的解决 ...