Replace To Make Regular Bracket Sequence

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
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible

题意:输入一行括号序列,左边括号可以和右括号抵消。右括号可以转换为其他类型右括号进行成对抵消。输出需要转换的个数。如果无法抵消输出"Impossible"
 #include <iostream>
#include <stack>
using namespace std; int main(){
string input;
while(cin >> input){
int count = ;
stack<char> s;
s.push(input[]); //[
for(int i = ;i < input.length();i++){//<}){}
if(s.size() > && (s.top() == '[' || s.top() == '<' || s.top() == '(' || s.top() == '{')){
if(input[i] == ']' || input[i] == '>' || input[i] == ')' || input[i] == '}'){
if(!((s.top() == '[' && input[i] == ']') ||
(s.top() == '<' && input[i] == '>') ||
(s.top() == '(' && input[i] == ')') ||
(s.top() == '{' && input[i] == '}'))){
count++;
}
s.pop();
}else{
s.push(input[i]);
}
}else{
s.push(input[i]);
}
}
if(s.size() != ){
cout << "Impossible" << endl;
}else{
cout << count << endl;
}
}
}

Replace To Make Regular Bracket Sequence的更多相关文章

  1. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  2. CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈

    C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...

  3. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  4. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence

    题目链接:http://codeforces.com/contest/612/problem/C 解题思路: 题意就是要求判断这个序列是否为RBS,每个开都要有一个和它对应的关,如:<()> ...

  5. CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】

    [链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...

  6. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  7. CF1095E Almost Regular Bracket Sequence

    题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...

  8. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  9. 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

    题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...

随机推荐

  1. less和sass的区别

    首先sass和less都是css的预编译处理语言,他们引入了mixins,参数,嵌套规则,运算,颜色,名字空间,作用域,JavaScript赋值等 加快了css开发效率,当然这两者都可以配合gulp和 ...

  2. tongweb安装后无法启动问题

    安装后执行bin下的启动文件,但是管理界面依然无法打开 1.确认破解的dat文件是否在跟目录下,是否过期 2.如果jdk64位试试下面的方法:JAVA_HOME/jre/lib/ext/下缺少包 su ...

  3. 如何用Fiddler手机抓包

    截获智能手机发出的HTTP包有什么用? 用处一: 手机软件程序员利用Fiddler,可以截获手机发出的HTTP包, 从而调试程序: 用处二: 软件测试人员用于测试智能手机上的软件: 用处三: 可以用来 ...

  4. python web编程之网络基础

    1.TCP/IP协议 1)分层 应用层,传输层,网络层,接口层 2)Ip地址 3)域名 4)URL统一资源定位符 格式:    [协议]://[主机]:[端口]/[路径]?[参数] 协议是HTTP,F ...

  5. jvm(一)类加载器

    1.jvm的生命周期结束的几种情况 a.执行了System.exit()方法 b.程序正常执行结束 c.程序在执行过程中遇到了异常或错误而异常终止 d.操作系统出现错误 2.类加载过程 加载:查找并加 ...

  6. jsfl 添加代码

    var __name=""; var __author=""; var __book=[]; var newJz=""; for(var d ...

  7. 根据设备width(375)动态设置font-size

    根据html的font-size使用rem来优化移动端页面 (function () { var w = window, d = document.documentElement, t; var re ...

  8. centos7 设置nginx和php开机自启动

    nginx开机自启动 首先我是源码安装的,需要手动建立nginx.service服务文件 cd /lib/systemd/system touch nginx.service nginx.servic ...

  9. docker 启动报错 WARNING: IPv4 forwarding is disabled. Networking will not work.

    解决办法: # vi /etc/sysctl.conf 或者 # vi /usr/lib/sysctl.d/00-system.conf 添加如下代码:     net.ipv4.ip_forward ...

  10. 解决mysql设置时区时的错误Unknown or incorrect time zone: 'Asia/Shanghai'

    Mysql默认时区格式是'+8:00'的格式,这个时区可以在my.ini中[mysqld]节点下设置 default-time-zone = '+8:00' 默认这个设置是没有的 但是mysql不支持 ...