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 ...
随机推荐
- 关于C语言的问卷调查
你对自己的未来有什么规划?做了哪些准备? 答:游戏开发,参与一些游戏的测试,通过自身的游戏体验和其他人的游戏体验来总结什么样的游戏会让人眼前一亮,爱不释手. 你认为什么是学习?学习有什么用?现在学习动 ...
- Hibdernate入门
Hibernate中java对象的三种状态 瞬时状态(Transient):通过NEW创建对象后对象并没有立刻持久化他未与数据哭中的数据有任何关联 持久状态(Persistent):当对象与Sessi ...
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- zookeeper安装
http://blog.itpub.net/27099995/viewspace-1394831/ http://blog.csdn.net/huwei2003/article/details/491 ...
- mac os 下搭建android开发环境
mac os 下搭建android开发环境 周银辉 mac os 下搭建android环境比较方便, 如下几个步骤: 1,安装jdk 先搞清楚自己是否已经安装,在命令行下:java -version, ...
- 使用axis2 soapmonitor监控soap数据
Using the SOAP Monitor SOAPMonitor使用 使用SOAPMonitor axis2开发笔记-消息监控 WebService大讲堂之Axis2(10):使用soapmoni ...
- 解决Scala Play框架在Git Bash运行的异常:Could not find configuration file ../framework/sbt/sbt.boot.properties
Git Bash+ConEmu可以模拟Linux强大的命令行.不过在结合Scala和Play时,需要注意如下事项: 1. Scala的安装在64位操作系统下,默认会放在“C:\Program File ...
- 超棒的javascript移动触摸设备开发类库-QUOjs
开发手机端网站.少不了手势事件? 手势事件怎么写? 手势事件怎么去判断? 对于新手来说.真的很Dan碎! 下面为大家推荐一款插件QUOjs 官方网站http://quojs.tapquo.com/ 这 ...
- async/await Task Timeout
async/await Task Timeout 在日常的电脑使用过程中,估计最难以忍受的就是软件界面"卡住""无响应",在我有限的开发生涯中一直都是在挑战 它 ...
- 利用python将二值csv格式转换为矩阵
#!/usr/bin/env python # coding:utf-8 #import pandas as pd, numpy as np; ''' 将csv文件转换为对应的邻接矩阵mat ''' ...