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;
}
CodeForces - 224C. Bracket Sequence (栈模拟)简单做法的更多相关文章
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维,模拟栈)
题意:给你一串括号,每次仅可以修改一个位置,问有多少位置仅修改一次后所有括号合法. 题解:我们用栈来将这串括号进行匹配,每成功匹配一对就将它们消去,因为题目要求仅修改一处使得所有括号合法,所以栈中最后 ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟
题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈
C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...
随机推荐
- vue技术栈进阶(02.路由详解—基础)
路由详解(一)--基础: 1)router-link和router-view组件 2)路由配置 3)JS操作路由
- 2059 - Authentication plugin 'caching_sha2_password' cannot be loaded dlope
今天在mac上使用navicat连接mysql报错弄了一下午,各种查询踩坑,总算解决了. 即从mysql5.7版本之后,默认采用了caching_sha2_password验证方式,我用的mysql8 ...
- Django 配置访问顺序 ->MTV开发模式
框架模式mvc m-->model 数据库 v-->view 视图 c-->controller 控件逻辑 mtv(django) m-->model 数据库 t--> ...
- C语言变长数组
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Variable ...
- k8s中token过期重新生成
k8s中token过期重新生成 通过kubeadm初始化之后,都会提供node加入的token 默认的token的有效期是24小时,当过期了,如何新生成呢 重新生成token: [root@k8s-m ...
- day23作业
# 作业: # 1.把登录与注册的密码都换成密文形式 info = {"tom":"202cb962ac59075b964b07152d234b70"} def ...
- Sprint1规划暨first stand up meeting
实际上,我们关于工程分配和接口实现的讨论已经好几周了,队(shen)长(xian)大人三令五申,先把接口确定下来,数据格式很重要云云~顺便accent一下,utf-8[虽然我并不太明白为什么要这么干但 ...
- 【jmeter】JDBC请求循环调用的问题
今天使用jdbc请求从数据库取数据,多次请求使用了循环控制器,但是结果第一个jdbc请求返回值正确,第二次请求返回值为空. 1.从其他博客中得知,需要在jdbc connection configur ...
- 中国剩余定理(CRT)
只看懂了CRT,EXCRT待补.... 心得:记不得这是第几次翻CRT了,每次都有迷迷糊糊的.. 中国剩余定理用来求解类似这样的方程组: 求解的过程中用到了同余方程. x=a1( mod x1) x= ...
- SSH、SCP命令及使用说明
SSH篇 1.ssh介绍 SSH是一种网络协议,用于计算机之间的加密登录.如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会泄露 ...