华工校赛 F. Bracket Sequence

F. Bracket Sequence

Time Limit: 1 Sec, Memory Limit: 128 MB

Description

A bracket sequence is a string,containing only characters "(", ")", "[" and"]". A correct bracket sequence is a bracket sequence that can betransformed into a correct arithmetic expression by inserting characters"1" and "+" between the original characters of thesequence. For example, bracket sequences "()[]", "([])" arecorrect (the resulting expressions are: "(1)+[1]","([1+1]+1)"), and "](" and "[" are not. The emptystring 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 strings) is the string slsl + 1... sr. The empty string is a substring of any stringby definition. You are given a bracket sequence, not necessarily correct. Findits substring which is a correct bracket sequence and contains as many openingsquare brackets «[» as possible.

Input

Multiple test cases. In the first linethere is an integer T, indicating the number of test cases. For each test case,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 lengthdoesn't exceed 106 characters.

Output

For each case, in the first line printa single integer — the number of brackets «[» in the required bracket sequence.In the second line print the optimal sequence. If there are more than oneoptimal solutions, choose the longest one. If there are still more than onesolutions, choose the one that occurs first.


//看到题目第一反应这应该是数据结构的题目,应该用栈来完成。然而队友告诉我是dp
//我觉得这道题的难点在于如何想到这是一道dp题。
//要有敏锐的直觉发现每个步骤之间的关系
//dp[i]:以str[i]结尾的最长合法子串长度
//key[i]:以str[i]结尾的最长合法子串有多少个'['
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int N = 2000080;
char str[N];
int dp[N], key[N];
int main()
{
#ifdef glx
	freopen("in.txt", "r", stdin);
#endif
	int t;
	cin >> t;
	while (t--)
	{
		memset(dp, 0, sizeof(dp));
		memset(key, 0, sizeof(key));
		memset(str, 0, sizeof(str));
		scanf("%s", str);
		int len = strlen(str);
		for (int i = 1; i < len; i++)
		{
			int pos = i - dp[i - 1] - 1;
			if (str[i] == ')')
			{
				if (str[pos] == '(')
				{
					dp[i] = dp[i - 1] + 2;
					key[i] = key[i - 1];
					if (dp[pos - 1] && pos >= 1)
					{
						dp[i] += dp[pos - 1];
						key[i] += key[pos - 1];
					}
				}
			}
			else if (str[i] == ']')
			{
				if (str[pos] == '[')
				{
					dp[i] = dp[i - 1] + 2;
					key[i] = key[i - 1] + 1;
					if (dp[pos - 1] && pos - 1 > 0)
					{
						dp[i] += dp[pos - 1];
						key[i] += key[pos - 1];
					}
				}
			}
		}
		int pos = -1, llen = -1, lnum = 0;
		for (int i = 0; i < len; i++)
		{
			if (key[i] > lnum || (dp[i] > llen&&key[i] == lnum))
			{
				pos = i - dp[i] + 1;
				llen = dp[i];
				lnum = key[i];
			}
		}
		cout << lnum << endl;
		if (lnum)
		{
			for (int i = pos; i < pos + llen; i++)
			{
				cout << str[i];
			}
			cout << endl;
		}
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值