首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】612C - Replace To Make Regular Bracket Sequence(栈,括号配对问题)

【CodeForces】612C - Replace To Make Regular Bracket Sequence(栈,括号配对问题)

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

C. Replace To Make Regular Bracket Sequence

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.

The following definition of a regular bracket sequence is well-known, so you can be familiar with it.

Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s1 and s2 be a RBS then the strings <s1>s2, {s1}s2, [s1]s2,(s1)s2 are also RBS.

For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.

Determine the least number of replaces to make the string s RBS.

Input

The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106.

Output

If it's impossible to get RBS from s print Impossible.

Otherwise print the least number of replaces needed to get RBS from s.

Examples

input

代码语言:javascript
代码运行次数:0
运行
复制
[<}){}

output

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

input

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

output

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

input

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

output

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

栈括号配对的题,本来不难,刚开始还是WA了。后来添加了两个变量 left 和 right ,记录左右括号的数量,若不相等,则说明没有完成配对。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
using namespace std;
char a[1000011];
int main()
{
	int l;
	int ans;
	bool dot;
	int left,right;
	while (~scanf ("%s",a))
	{
		l=strlen(a);
		stack<char>s;
		dot=true;
		ans=0;
		int left=0,right=0;
		for (int i=0;i<l;i++)
		{
			if (a[i]=='(' || a[i]=='<' || a[i]=='{' || a[i]=='[')
			{
				s.push(a[i]);
				left++;
			}
			else if (a[i]==')')
			{
				right++;
				if (s.empty())
				{
					dot=false;
					break;
				}
				else if (s.top()=='(')
				{
					s.pop();
				}
				else
				{
					ans++;
					s.pop();
				}
			}
			else if (a[i]==']')
			{
				right++;
				if (s.empty())
				{
					dot=false;
					break;
				}
				else if (s.top()=='[')
				{
					s.pop();
				}
				else
				{
					ans++;
					s.pop();
				}
			}
			else if (a[i]=='}')
			{
				right++;
				if (s.empty())
				{
					dot=false;
					break;
				}
				else if (s.top()=='{')
				{
					s.pop();
				}
				else
				{
					ans++;
					s.pop();
				}
			}
			else if (a[i]=='>')
			{
				right++;
				if (s.empty())
				{
					dot=false;
					break;
				}
				else if (s.top()=='<')
				{
					s.pop();
				}
				else
				{
					ans++;
					s.pop();
				}
			}
		}
		if (left!=right)
			dot=false;
		if (dot)
			printf ("%d\n",ans);
		else
			printf ("Impossible\n");
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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