首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】223A - Bracket Sequence(栈 & 模拟)

【CodeForces】223A - Bracket Sequence(栈 & 模拟)

作者头像
FishWang
发布2025-08-26 20:29:48
发布2025-08-26 20:29:48
9800
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

A. Bracket Sequence

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

代码语言:javascript
代码运行次数:0
运行
复制
([])

output

代码语言:javascript
代码运行次数:0
运行
复制
1
([])

input

代码语言:javascript
代码运行次数:0
运行
复制
(((

output

代码语言:javascript
代码运行次数:0
运行
复制
0

这道题做起来好烦啊,说一下思路吧:

像平时括号配对问题一样,能配对的弹出,否则就压入栈(下标),最后得到的栈两两数中间是能配对的(不包括开头结尾),然后依次取数算次数就行了。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <map>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
	map<char,int> m;
	m['('] = 1;
	m[')'] = -1;
	m['['] = 2;
	m[']'] = -2;
	char str[100000+22];
	int l;
	int ans;
	while (~scanf ("%s",str))
	{
		l = strlen(str);
		stack<int> s;
		ans = 0;
		for (int i = 0 ; i < l ; i++)
		{
			if (m[str[i]] == 1 || m[str[i]] == 2)
				s.push(i);
			else if (s.empty())
				s.push(i);
			else if (m[str[i]] + m[str[s.top()]] == 0)		//可以配对,弹出
				s.pop();
			else		//不能配对,压入 
				s.push(i);
		}
		
		if (s.empty())		//全部完成匹配 
		{
			for (int i = 0 ; i < l ; i++)
				if (m[str[i]] == 2)
					ans++;
			printf ("%d\n%s\n",ans,str);
		}
		else
		{
			int st,endd;		
			int ans_st,ans_endd;		//满足条件的开始于结束位置
			int ant;
			s.push(l);
			while (s.size() != 1)
			{
				endd = s.top() - 1;
				s.pop();
				st = s.top() + 1;
				ant = 0;
				for (int i = st ; i <= endd ; i++)
					if (m[str[i]] == 2)
						ant++;
				if (ant > ans)
				{
					ans = ant;
					ans_st = st;
					ans_endd = endd;
				}
			}
			st = 0;
			endd = s.top() - 1;
			ant = 0;
			for (int i = st ; i <= endd ; i++)
				if (m[str[i]] == 2)
					ant++;
			if (ant > ans)
			{
				ans = ant;
				ans_st = st;
				ans_endd = endd;
			}
			if (ans)
			{
				printf ("%d\n",ans);
				for (int i = ans_st ; i <= ans_endd ; i++)
					printf ("%c",str[i]);
				printf ("\n");
			}
			else
				printf ("0\n\n");
		}
	}
	return 0;
} 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档