CodeForces - 224C. Bracket Sequence (栈模拟)简单做法
A bracket sequence is a string, containing only characters “(”, “)”, “[” and “]”.
A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters “1” and “+” between the original characters of the sequence. For example, bracket sequences “()[]”, “([])” are correct (the resulting expressions are: “(1)+[1]”, “([1+1]+1)”), and “](” and “[” are not. The empty string is a correct bracket sequence by definition.
A substring s[l… r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2… s|s| (where |s| is the length of string s) is the string slsl + 1… sr. The empty string is a substring of any string by definition.
You are given a bracket sequence, not necessarily correct. Find its substring which is a correct bracket sequence and contains as many opening square brackets «[» as possible.
Input The first and the only line contains the bracket sequence as a string, consisting only of characters “(”, “)”, “[” and “]”. It is guaranteed that the string is non-empty and its length doesn’t exceed 105 characters.
Output In the first line print a single integer — the number of brackets «[» in the required bracket sequence. In the second line print the optimal sequence. If there are more than one optimal solutions print any of them. Examples
Input ([]) Output 1 ([]) Input ((( Output 0 括号是就近匹配的,所以可以用栈来模拟,所以可以将括号压栈,匹配后出栈,最后栈底剩余的就是不能出栈的就是不能匹配的,一般的方法是找到这些括号但是太费劲了,我们同时建立一个栈,同时入栈,出栈,存括号的下标,那么在出栈操作之后,第一个stack就只剩下不匹配的括号,第二个stack就只剩下不匹配的括号的下标。 下标将括号数组分成了好几段,枚举每一段的左中括号的数量即可,比较最大值更新左右段点即可
#include <bits/stdc++.h>
using namespace std;
const int maxn=100005;
int b[maxn]={0};
stack<int>demo;
stack<int>de;
vector<int>ans;
string a;
int main()
{
while(!demo.empty())demo.pop();
while(!de.empty())de.pop();
ans.clear();
//初始化操作
cin>>a;
int n=a.size();
for(int i=0;i<a.size();i++)
{
if(a[i]=='(') b[i]=-2;
else if(a[i]==')') b[i]=2;
else if(a[i]=='[') b[i]=-1;
else b[i]=1;
}
for(int i=0;i<n;i++){
if(demo.empty()||b[i]==-1||b[i]==-2)
{
demo.push(b[i]);
de.push(i);
}
else if(b[i]+demo.top()==0)
{
demo.pop();
de.pop();
}
else{
demo.push(a[i]);
de.push(i);
}
}
if(demo.empty())
{
int res=0;
for(int i=0;i<a.size();i++) if(a[i]=='[') res++;
cout<<res<<endl<<a<<endl;
return 0;
}
while(!de.empty())
{
ans.push_back(de.top());
de.pop();
}
ans.push_back(n);//补足区间
sort(ans.begin(),ans.end());
int l,r=-1, /*补足区间*/ml,mr,res=0,maxi=0;
for(int i=0;i<ans.size();i++)
{
l=r+1;
r=ans[i];
res=0;
for(int i=l;i<r;i++)
{
if(a[i]=='[') res++;
}
if(res>maxi)
{
ml=l;mr=r-1;
maxi=res;
}
}
if(maxi==0){
cout<<0<<endl;
return 0;
}
cout<<maxi<<endl;
for(int i=ml;i<=mr;i++) cout<<a[i];
cout<<endl;
}
- 无特性的 MEF 配置方法
- HTTP协议状态码详解(HTTP Status Code)
- android 中resources管理
- 你不知道的javaScript笔记(4)
- Android网格视图(GridView)
- http响应Last-Modified和ETag以及asp.net web api实现
- 列表视图(ListView和ListActivity)
- 你不知道的javaScript笔记(3)
- 你不知道的javaScript笔记(2)
- WinRT:WebAuthenticationBroker For OAuth认证
- 你不知道的javaScript笔记(1)
- JQuery实现仿sina新浪微博新鲜事滚动
- 简单的jquery拖曵原理js特效实例
- 使用MiniProfiler调试ASP.NET MVC网站性能
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法