博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
阅读量:6404 次
发布时间:2019-06-23

本文共 1929 字,大约阅读时间需要 6 分钟。

C. Replace To Make Regular Bracket Sequence

题目连接:

Description

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

Sample Input

[<}){}

Sample Output

2

Hint

题意

给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号

问你最少修改多少次,才能使得这个字符串匹配,输出次数

题解:

用stack,每次将左括号压进stack里面,遇到右括号就判断一下就好了

非法就很简单,看看栈最后是否还有,看看右括号的时候,左括号的栈是否为空

代码

#include
using namespace std;string s;stack
S;int main(){ cin>>s; int ans = 0; for(int i=0;i
') { if(S.size()==0)return puts("Impossible"); if(S.top()=='<') S.pop(); else { ans++; S.pop(); } } else if(s[i]=='}') { if(S.size()==0)return puts("Impossible"); if(S.top()=='{') S.pop(); else { ans++; S.pop(); } } else S.push(s[i]); } if(S.size()!=0)return puts("Impossible"); cout<
<

转载地址:http://aljea.baihongyu.com/

你可能感兴趣的文章
C# DES加密
查看>>
浅谈Oracle分区表之范围分区
查看>>
IBM Tivoli NetView网管软件实战
查看>>
IPSec逻辑体系架构
查看>>
Exchange 2013部署系列之(六)配置邮件流和客户端访问
查看>>
List of Free Programming books
查看>>
思考Android架构(二):像Android框架,如何(How-to)吸引开发者来使用它呢?
查看>>
在html中,怎么获取当前页面body的高度,body是没有设置高度的,但是里面有内容...
查看>>
把 Array 转换成 Map
查看>>
MyBatis入门学习
查看>>
ASA防火墙IPSEC
查看>>
djangostart01
查看>>
Ubuntu 12.04无法关机、重启解决办法
查看>>
Tomcat的四种基于HTTP协议的Connector性能比较
查看>>
【后缀数组】
查看>>
图片缩放裁剪
查看>>
jquery ajax 回调函数的值alert出来[object Object] 解决方法
查看>>
JQuery选择器总结
查看>>
MySQL安装详解(V5.5 For Windows)
查看>>
Android单例模式
查看>>