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 ...
随机推荐
- MFC 单选按钮Radio使用注意
使用MFC Radio时遇到问题:数据交换时出现断言崩溃框 定位于: 解决方法: 1.按CTRL+D,保证同一组内的radio的tab序号是连续的: 2.同一组内,设置 radio1的属性: gro ...
- Appium自动化测试3之获取apk包名和launcherActivity后续
接着“Appium自动化测试3之获取apk包名和launcherActivity”章节介绍 测试脚本 1.测试脚本如下: # -*- coding:utf-8 -*- import os, time, ...
- 用原生Canvas写贪吃蛇及问题解决
为了学习Canvas,写了这个小游戏贪吃蛇供自己和大家学习 Github: https://github.com/zhiyishou/Gsnake Play On: http://zhiyishou. ...
- TreeSet排序,存储自己定义对象,自己定义比較器演示样例
Set:无序.不能够反复元素. |--HashSet:数据结构是哈希表.线程是非同步的. 保证元素唯一性的原理:推断元素的hashCode值是否同样. 假设同样,还会继续推断元素的equals方法.是 ...
- iOS开发之 -- CocoPods的安装和使用
以前项目中使用过Pods 1.0,最近项目中也有用到,因为很长时间没用了,所以配置了下,谁知道,中间出现了不少坑,特记录在此博客,其实pods的使用也就是几个终端命令而已,可能随着系统的升级,以前的终 ...
- 《转》武汉的IT公司
本文转载自sherry020406前段时间看到版上有同学问在武汉找工作的情况,我谈谈去年找工作时碰到或者听到的一些企业,希望以下内容对想去武汉工作的同学有帮助,也算是对job版的回报.有些公司的情况可 ...
- MFC中控件的TAB顺序
本文来自: http://hi.baidu.com/qingcaichongch/item/47f7ae14de8cbef6ddeeca42 在MFC中添加控件后,按Ctrl+d可以改变控件TAB顺序 ...
- Map中存放数组
Map<String,Object> map = new HashMap<String, Object>(); Map<String,String> agentMa ...
- Hibernate如何执行存储过程?
Hibernate如何执行存储过程? @Overridepublic Boolean setVarValue(final String processInstanceId, final String ...
- springmvc常用注解标签详解(转载)
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...