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. 7种清除浮动 (感觉br最好用然而我用的还是overflow)

    1.clear清除浮动(添加空div法) 在浮动元素下方添加空div,并给该元素写css样式: {clear:both;height:0;overflow:hidden;} 2.方法:给浮动元素父级设 ...

  2. jq中的$操作符与其他js框架冲突

    解决办法: jq中存在方法:noConflict() 可返回对 jQuery 的引用. 使用示例: var jq = $.noConflict(); jq(document).ready(functi ...

  3. spring boot tomcat 打本地包成war,通过Tomcat启动时出现问题: ZipException: error in opening zip file

    一个第三方公司提供spring boot 项目,直接启动是ok的, 但是打包成war,通过Tomcat启动,就出现 ZipException: error in opening zip file: 2 ...

  4. mybatis学习 -每天一记(驼峰命名匹配)

    在mybatis 中,数据库表有一个与之对应的实体类.类属性的命名是驼峰命名的,所以在mybatis中要开启驼峰匹配, 在spring boot 的项目中,至需要在yml文件中配置 即可. 当然也有其 ...

  5. 深度学习实践-物体检测-faster-RCNN(原理和部分代码说明) 1.tf.image.resize_and_crop(根据比例取出特征层,进行维度变化) 2.tf.slice(数据切片) 3.x.argsort()(对数据进行排列,返回索引值) 4.np.empty(生成空矩阵) 5.np.meshgrid(生成二维数据) 6.np.where(符合条件的索引) 7.tf.gather取值

    1. tf.image.resize_and_crop(net, bbox, 256, [14, 14], name)  # 根据bbox的y1,x1,y2,x2获得net中的位置,将其转换为14*1 ...

  6. js实现reqire中的amd,cmd功能

    js实现reqire中的amd,cmd功能 ,大概实现了 路径和模块 引入等重要功能. 本帖属于原创,转载请出名出处. <!DOCTYPE html PUBLIC "-//W3C//D ...

  7. pywin32安装教程

    下载 pywin32-224-cp37-cp37m-win32.whl 放在 Scripts 目录下,然后cmd 安装 pip install pypiwin32 即可成功 此次安装成功对应的pyth ...

  8. MySQL 8.0 中统计信息直方图的尝试

    直方图是表上某个字段在按照一定百分比和规律采样后的数据分布的一种描述,最重要的作用之一就是根据查询条件,预估符合条件的数据量,为sql执行计划的生成提供重要的依据在MySQL 8.0之前的版本中,My ...

  9. Spring再接触 整合Hibernate

    首先更改配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  10. Bootstrap col-md

    col-md  将中等屏幕分成12份, 所占的比列 offest 补偿,向左偏移 push 从左侧往右侧推8列 pull  往左侧拉2列