Time Limit:2000MS     Memory Limit:65536KB

Description

Given a string consisting of brackets of two types find its longest substring that is a regular brackets sequence.

Input

There are mutiple cases in the input file.

Each case contains a string containing only characters ‘(’ , ‘)’ , ‘[’ and ‘]’ . The length of the string does not exceed 100,000.

There is an empty line after each case.

Output

Output the longest substring of the given string that is a regular brackets sequence.

There should be am empty line after each case.

Sample Input

([(][()]]()

([)]

Sample Output

[()]

题解:

这道题是用栈进行括号的匹配过程。具体思路如下:

1、使用字符串str[100005]储存输入的括号,遍历操作。每个字符有三种情况,(1)“(]”或“[)”属于无法匹配,之前栈中的括号无法进行后续匹配,所以进行清空栈。(2)当前遍历到的字符可以与栈顶元素抵消,那么此时用到了一个标记数组p,将栈顶元素和当前元素位置标记为1。(3)不属于前两种情况,进栈等待匹配。由函数 can_place(char)进行判断。

2、将匹配位置都置1后,需要判断全为1最长子串,这很容易,使用st更新起始位置,maxl更新长度即可。

 以下是代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <cstring>
#include <utility>
using namespace std;
char str[100005];
int p[100005];//标记括号成功匹配
stack<pair<char,int> >v;
int can_place(char ch){//如果非法,返回0,可进栈,返回1,可与栈顶抵消,返回2
if(v.empty()){
if(ch == '(' || ch == '[')return 1;
return 0;
}else switch(v.top().first){
case'(':if(ch ==']')return 0;if(ch==')')return 2;return 1;
case'[':if(ch==')')return 0;if(ch==']')return 2;return 1;
}
}
int main(){
//freopen("1.in","r",stdin);
int len;
while(scanf("%s",str)!=EOF){
len = strlen(str);
memset(p,0,sizeof(p));
for(int i=0;i<len;i++)
switch(can_place(str[i])){
case 0:while(!v.empty())v.pop();break;//遇到非法括号,清空栈
case 1:v.push(make_pair(str[i],i));break;//若可以进栈,进栈
case 2:p[i]=p[v.top().second]=1;v.pop();//若匹配出栈,将对应位置置1
}
while(!v.empty())v.pop();
int maxl=0,tl=0,t=0,st=0;
for(int i=0;i<len;i++){//计算最长的全1子串
if(!p[i]){
t=i+1;
tl=0;
}else tl++;
if(maxl<tl){//更新长度maxl及开始位置st
st=t;
maxl=tl;
}
}
for(int i=st;i< st+maxl;i++)
printf("%c",str[i]);
printf("\n\n");
}
}

以下是测试数据:

smaple input

[](()](()[()])

()()[[](]

[][()()][(])

​sample output

(()[()])

()()

[][()()]

Winter-2-STL-B Brackets 解题报告及测试数据的更多相关文章

  1. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

  2. Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据

    Array Diversity Time Limit:404MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descript ...

  3. Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据

    Goblin Wars Time Limit:432MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Th ...

  4. Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据

    Save the Students Time Limit:134MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descri ...

  5. Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据

    Magic Grid Time Limit:336MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Tha ...

  6. Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据

    233 Matrix Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Descript ...

  7. Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

    Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Pro ...

  8. Spring-1-F Dice(HDU 5012)解题报告及测试数据

    Dice Time Limit:1000MS     Memory Limit:65536KB Description There are 2 special dices on the table. ...

  9. Spring-1-E Game(HDU 5011)解题报告及测试数据

    Game Time Limit:1000MS     Memory Limit:65536KB Description Here is a game for two players. The rule ...

随机推荐

  1. hdu 3599(最短路+最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3599 思路:首先spfa求一下最短路,然后对于满足最短路上的边(dist[v]==dist[u]+w) ...

  2. 介绍MFC框架中涉及到的设计模式(二)

    接着上一篇<介绍MFC框架中涉及到的设计模式(一)>介绍 单例模式(Singleton Pattern) 单例模式是一种经常使用的软件设计模式.在它的核心结构中仅仅包括一个被称为单例类的特 ...

  3. Java Tomcat 注册为Windows系统服务

    注册方法: 1. 在DOS命令行模式下,cd到tomcat的bin目录下 cd tomcatpath 根目录加:后回车 进入到tomcat安装目录,cd bin,进入tomcat启动目录 2.在tom ...

  4. JZOJ.5306【NOIP2017模拟8.18】棋盘游戏

    Description 这个游戏上在一个无限大的棋盘上, 棋盘上只有一颗棋子在位置(x,y)(x,y>=0)棋盘的左下角是(0,0)Amphetamine每次都是第一个移动棋子,然后Amphet ...

  5. 【BZOJ1005/1211】[HNOI2008]明明的烦恼/[HNOI2004]树的计数 Prufer序列+高精度

    [BZOJ1005][HNOI2008]明明的烦恼 Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可 ...

  6. ios sqlite的创建数据库,表,插入查看数据

    iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1.新建项目sqliteDemo,添 ...

  7. MAC OSX安装多个版本的JAVA(jdk jre通用)

    MAC自带的jdk1.6是苹果公司自己修改的jdk版本,被广泛应用于各种mac软件,具有不可替代性:同时,java1.7和1.8有时也需要用到.因此,在mac上安装.使用多个版本的java具有重要意义 ...

  8. CSS文本对齐text-align详解

    1.语法 text-align具体参数如下: 语法:text-align : left | right | center | justify 说明:设定元素内文本的水平对齐方式. 参数:left :  ...

  9. 170419、Centos7下完美安装并配置mysql5.6

    首先跟各位说声抱歉,原计划说每天一篇博文,最近由于实在太忙,封闭式开发一个项目,没有时间写博文,望大家见谅!!! 由于公司要搭建分布式服务,我把最近我所用到或者学习的技术或者遇到的问题跟大家分享一下! ...

  10. Mustache 中的html转义问题处理

    避免在使用Mustache引擎是发生html字符转义 1,模板代码示例:    var xml= " <?xml version="1.0" encoding=&q ...