Winter-2-STL-B Brackets 解题报告及测试数据
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 解题报告及测试数据的更多相关文章
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据
Array Diversity Time Limit:404MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据
Goblin Wars Time Limit:432MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Th ...
- Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据
Save the Students Time Limit:134MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据
Magic Grid Time Limit:336MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Tha ...
- Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据
233 Matrix Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descript ...
- Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据
Number Sequence Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Pro ...
- Spring-1-F Dice(HDU 5012)解题报告及测试数据
Dice Time Limit:1000MS Memory Limit:65536KB Description There are 2 special dices on the table. ...
- Spring-1-E Game(HDU 5011)解题报告及测试数据
Game Time Limit:1000MS Memory Limit:65536KB Description Here is a game for two players. The rule ...
随机推荐
- 第三篇:CUDA 标准编程模式
前言 本文将介绍 CUDA 编程的基本模式,所有 CUDA 程序都基于此模式编写,即使是调用库,库的底层也是这个模式实现的. 模式描述 1. 定义需要在 device 端执行的核函数.( 函数声明前加 ...
- 用DX9画三角形,三个顶点要求
用DX9画三角形,三个顶点的顺序有要求吗下面是全部代码,在 InitVB 函数中被注释掉的数组是我写的,没注释掉的是书上的代码,经过试验我的坐标不能显示出图形,而书上的坐标可以,这是为什么? #i ...
- python反序列化研究学习
零.补充: 补充于2018-02-08,之前研究时候有一个疑惑,python的序列化成二进制,打web服务怎么传这个二进制对象呢,今天请教了身边大神(传说的九零后黑客代表),可以使用base64传输. ...
- Cocos2d-x学习之 整体框架描述
1.Cocos2d-x 整体描述 和传统的游戏引擎一样,cocos2d-x作为一个2d的游戏引擎,其也有以下几大概念组成: 导演(CCDiretor): 在cocos2d-x引擎中,CCDirecto ...
- Vue基础-渲染函数-父子组件-传递数据
Vue 测试版本:Vue.js v2.5.13 做了个 demo,把父子组件的数据都绑定到 Vue 实例 app 上,注释中的 template 相对好理解些 <div id="app ...
- 170316、spring4:@Cacheable和@CacheEvict实现缓存及集成redis
注:1.放入cache中,采用@Cacheable;使缓存失效@CacheEvict 2.自定义CacheManager只需要继承org.springframework.cache.support.A ...
- 基于spring的shiro配置
shiro是一个特别简单,易用的框架,在此记录一下shiro的使用配置. 首先,创建四张表:user role user_role permission,分别为用户.角色.用户与角色关系表和权限 ...
- iOS 10 获取相册相机权限
AVAudioSession *audioSession = [[AVAudioSession alloc]init]; [audioSession requestRecordPerm ...
- Network Security Services If you want to add support for SSL, S/MIME, or other Internet security standards to your application, you can use Network Security Services (NSS) to implement all your securi
Network Security Services | MDN https://developer.mozilla.org/zh-CN/docs/NSS 网络安全服务 (NSS) 是一组旨在支持支持安 ...
- echarts容器动态设置高度
测试提了bug,柱状图数据多的情况下,都叠到了一起,效果如下图. 要解决这个bug,首先想到的是让柱状图的容器自适应高度.于是,把原本div上写固定的高度去掉. <div id="my ...