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.

Examples

Input

[<}){}

Output

2

Input

{()}[]

Output

0

Input

]]

Output

Impossible

意思是有左右两类括号,同类可以变形,求把所有括号消掉需要变形多少次,不可以消掉就输出impossible,加个例子{[}]这个是要两次;用stack栈

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include <iomanip>
  5. #include<cmath>
  6. #include<float.h>
  7. #include<string.h>
  8. #include<algorithm>
  9. #define sf scanf
  10. #define pf printf
  11. #define mm(x,b) memset((x),(b),sizeof(x))
  12. #include<vector>
  13. #include<queue>
  14. #include<stack>
  15. #include<map>
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=a;i>=n;i--)
  18. typedef long long ll;
  19. typedef long double ld;
  20. typedef double db;
  21. const ll mod=1e9+100;
  22. const db e=exp(1);
  23. using namespace std;
  24. const double pi=acos(-1.0);
  25. char a[1000005];
  26. int judge(int n)
  27. {
  28. stack<char>v;
  29. int num1=0,num2=0,ans=0;
  30. rep(i,0,n)
  31. {
  32. if(a[i]=='['||a[i]=='('||a[i]=='{'||a[i]=='<')
  33. v.push(a[i]);
  34. else
  35. {
  36. if(v.empty()) return -1;
  37. switch(a[i])
  38. {
  39. case '>':if(v.top()!='<') ans++;break;
  40. case ']':if(v.top()!='[') ans++; break;
  41. case ')':if(v.top()!='(') ans++; break;
  42. case '}':if(v.top()!='{') ans++; break;
  43. }
  44. v.pop();
  45. }
  46. }
  47. if(!v.empty()) return -1;
  48. return ans;
  49. }
  50. int main()
  51. {
  52. sf("%s",a);
  53. int len=strlen(a);
  54. if(len&1) { pf("Impossible"); return 0; }
  55. if(judge(len)==-1) pf("Impossible");
  56. else pf("%d",judge(len));
  57. return 0;
  58. }

D - 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. Replace To Make Regular Bracket Sequence

    Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...

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

  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. linux-批量杀死进程

    kill `ps -ef|grep 进程名 | grep -v grep|awk '{print $2}'` 例如: kill `ps -ef | grep /etc/pam.d/su |grep - ...

  2. WIN10平板系统 如何自定义分辨率,修改分辨率

    从以下网址下载Intel网卡驱动,注意只要下载zip版本的(如果是exe版本的,双击将提示win10无法为此计算机验证正在安装的驱动程序,也包括你用驱动精灵这种软件,也会安装的时候由于这个问题装不上) ...

  3. 使用JavaCV播放视频、摄像头、人脸识别

    一.导入Maven依赖包 <dependencies> <!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-pla ...

  4. [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?

    在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...

  5. How to extract a complete list of extension types within a directory?

    Open the PowerShell Tool and Run the below command: Get-Childitem "D:\testfolder\" -Recurs ...

  6. 【C语言】字节对齐问题(以32位系统为例)

    1. 什么是对齐? 现代计算机中内存空间都是按照字节(byte)划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型 ...

  7. Sql Server 阻塞的常见原因和解决办法

    1. 由于语句运行时间太长而导致的阻塞,语句本身在正常运行中,只须等待某些系统资源 解决办法: a. 语句本身有没有可优化的空间 b. Sql Server 整体性能如何,是不是有资源瓶颈影响了语句执 ...

  8. MapReduce实现矩阵乘法

    简单回想一下矩阵乘法: 矩阵乘法要求左矩阵的列数与右矩阵的行数相等.m×n的矩阵A,与n×p的矩阵B相乘,结果为m×p的矩阵C.具体内容能够查看:矩阵乘法. 为了方便描写叙述,先进行如果: 矩阵A的行 ...

  9. vue cli 项目的提交

    前提: 配置git.以及git的ssh key信息 假设已经都安装好了,此处我用vue项目为例,因为vue-cli已经默认为我生成了ignore文件 在项目目录 初始化本地仓库,会创建一个.git目录 ...

  10. java框架篇---hibernate(一对一)映射关系

    对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术,本质上就是将数据从 ...