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. day44前端开发2之css基础

    web前端开发1一.前端三剑客之css 1.选择器:由标签/类/id单独或组合出现 2.作用域:{}内部区域 3.样式块:满足css链接语法的各种样式 eg:引入的基本样式 <head>  ...

  2. 关于对CSS中超链接那部分的设置

    a:link{                                  //正常下的超链接 color:red;                          //超链接的颜色 text ...

  3. Android 开发 Activity里获取View的宽度和高度 转载

    原文地址:https://blog.csdn.net/chenbaige/article/details/77991594 前言: 可能很多情况下,我们都会有在activity中获取view 的尺寸大 ...

  4. py库:numpy

    http://www.numpy.org/ numpy官网 http://cwiki.apachecn.org/pages/viewpage.action?pageId=10030181 scikit ...

  5. C#控件——批量化隐藏或显示同类型控件

    当一个页面中添加了许多同类型控件,当需要控制这些控件进行显示或隐藏的时候,需要一个个的将Visible属性设置为false,十分不方便, 后通过论坛受一位大神(至于叫什么忘了)的启发,通过建立控件数组 ...

  6. java学习-- String

    String 类的实例是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了 String 类是不可改变的解析,例如: String s = "Google"; ...

  7. php使用coreseek进行中文分词搜索

    方法一 使用coreseek源码自带testpack/api/test_coreseek.php代码,进行稍微修改就可以使用了,只不过需要引入”spinxapi.php“类 方法二--制作php扩展 ...

  8. git 琐碎

    git symbolic-ref --short HEAD 来获取对应 HEAD 的分支名 ➜ mis-gulf git:(mis-lk) ✗ git symbolic-ref --short HEA ...

  9. could not open input file 错误

    配置laravel时遇到的小错误 ps:php -S localhost:81 -t 框架目录/public S大写 端口不要被占用

  10. OO第一单元小结

    写在前面 在接触OO课程之前,自己是完全没有学习过java语言的,因此作为一名初的不能再初的初学者,无论是在哪方面都会有许多茫然,但是我相信通过一次次认真的完成OO作业,我对面向对象的理解应该会渐渐的 ...