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. 类库服务寄宿到WebHost

    1.该Demo中包含一个类库项目.一个空的WebForm项目 2.新建WebForm项目 3.全局路由中注册类库服务 public class Global : System.Web.HttpAppl ...

  2. win10 设置C盘访问权限

    “以管理员身份运行” cmd.exe C:\Windows\system32>icacls "C:\Program Files\Epic Games" /setintegri ...

  3. 《C#高级编程》学习笔记----c#内存管理--栈VS堆

    本文转载自Netprawn,原文英文版地址 尽管在.net framework中我们不太需要关注内存管理和垃圾回收这方面的问题,但是出于提高我们应用程序性能的目的,在我们的脑子里还是需要有这方面的意识 ...

  4. [libwww-perl]——POST方法的使用

    libwww-perl是我在学习varnish的时候遇到的一个工具. 具体libwww-perl是干什么的,可以参考官网https://github.com/libwww-perl/libwww-pe ...

  5. [Go语言]从Docker源码学习Go——Interfaces

    Interface定义: type Namer interface { Method1(param_list) return_type Method2(param_list) return_type ...

  6. linux的简单查找的方法

    catalina.out文件查找指定行sed -n 346492p catalina.out 查找第几到第几行sed -n 346200,346692p catalina.out 查找指定内容(不区分 ...

  7. Android Activity 去掉标题栏及全屏显示

    默认生成的活动(Activity)界面中包含标题栏,并带有状态栏.有时不需要这两个控件. 1.去掉标题栏 (三种方法) a:在setContentView()方法前 添加:requestWindowF ...

  8. flume jetty 进程关系 flume jetty 跨域问题 jetty 源码分析

    flume  jetty  跨域问题 13481 httpSource的端口进程号 = flume 启动后的进程号 [root@c log]# netstat -atp Active Internet ...

  9. Linux Shell 文本处理工具集锦--Awk―sed―cut(row-based, column-based),find、grep、xargs、sort、uniq、tr、cut、paste、wc

    本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实用的: ...

  10. CSS语义化命名

    CSS语义化命名 从上图我们可以大概看出这里有两种CSS的命名方式:1.结构化命名法:2.语义化命名法. 结构化命名法:根据页面中板块的位置而命名,如上图中的content-left,这时如果我们想把 ...