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

探讨括号匹配问题中的最优子序列寻找方法,通过栈结构实现有效的匹配过程,并找到包含最多方括号的正确括号序列。

点击打开题目

A. Bracket Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

bracket sequence is a string, containing only characters "(", ")", "[" and "]".

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.

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... srThe 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



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

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


代码如下:

#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;
} 


### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner (&#39;A&#39; or &#39;B&#39;) based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = &quot;A&quot; if count_map[&#39;A&#39;] &gt; count_map[&#39;B&#39;] else &quot;B&quot; print(result) determine_winner() ``` This approach leverages `Counter` from Python&rsquo;s built-in `collections` module to quickly tally up instances of &#39;A&#39; versus &#39;B&#39;. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值