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 ...
随机推荐
- django admin后台css样式丢失
尼玛 坑爹啊 怎么光秃秃的,跟人家的不一样啊 打开firebug 发现报错,找不到css 通过google找到原因,是因为admin所需的js ,css等静态文件虽然都在django的安装目录内,但是 ...
- 配置使用TargetFrameworks输出多版本类库
1.类库右键 2.修改配置 修改前: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <Targe ...
- 关于Bootstrap的理解
Web开发领域存在大量的反复劳动.以创建一个菜单为例,不同的人或是同一个人在不同的时期去构建一个菜单.他创建出来的菜单格式都会存在差异:随着构件的菜单越来越多,我们会发现假设将构建菜单这件事形成一个框 ...
- swift - storyboard(故事版)的使用
iOS开发中,苹果公司提供了一种可视化的编程方式:即xib和storyboard,xib相对来说比较灵活,可以在纯代码的项目中使用, 也可以和storyboard配合使用,用法都差不多,下面来总结一下 ...
- 使用 composer 下载更新卸载类库
前言:要下载什么包,可以去 https://packagist.org/ 找一下包名及其版本信息 1)配置composer.json文件,并使用composer install 命令下载类包,下面以下 ...
- 【RF库测试】Exit For Loop 相关
1.Exit For Loop If:满足条件时,跳出循环,后面的循环不再执行 2.Continue For Loop If:满足条件时,跳出本次循环,继续执行后面的循环
- AVR 定时器快速PWM模式使用
PWM很常用,AVR自带内部PWM功能,分为快速PWM模式和相位修正PWM模式. 我们这里选择方式15 ,由OCR1A保存上限值,由OCR1B保存匹配值,所以输出管脚 OCR1A不能输PWM,只能 ...
- C语言条件运算符
如果希望获得两个数中最大的一个,可以使用 if 语句,例如: if(a>b){ max = a; }else{ max = b; } 不过,C语言提供了一种更加简单的方法,叫做条件运算符,语法格 ...
- 移动端form表单
始终绑定submit事件 不单独的对[提交]按钮绑定click事件,对整个表单绑定submit提交事件,这样可以让整个表单内的文本框获得Enter提交的VIP待遇,并且在移动端中可以让文本框聚焦时键盘 ...
- js Tab切换实例
js 实现 tab 切换 实现如下效果: 1.图片每1秒钟切换1次. 2.当鼠标停留在整个页面上时,图片不进行轮播. 3.当点击切换页的选项上时,出现该选项的对应图片,而且切换页选项的背景颜色发生相应 ...